import imagereader.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class imageIO implements IImageIO {
class BitmapFileHeader{
char[] bfType;
int bfSize;
int bfReserved;
int bfOffBits;
public BitmapFileHeader(){
bfType = new char[2];
}
}
class BitmapInfoHeader{
int biSize;
int biWidth;
int biHeight;
short biPlanes;
short biBitCount;
int biCompression;
int biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
int biClrUsed;
int biClrImportant;
}
private BitmapFileHeader head;
private BitmapInfoHeader inform;
private byte[] rgb;
private int[] data;
public imageIO(){
rgb = null;
data = null;
}
public Image myRead(String filePath){
try{
FileInputStream file = new FileInputStream(filePath);
int bfLen = 14;
int biLen = 40;
byte[] header = new byte[bfLen];
file.read(header, 0, bfLen);
byte[] info = new byte[biLen];
file.read(info, 0, biLen);
head = getFileHeader(header);
inform = getInfoHeader(info);
int width = inform.biWidth;
int height = inform.biHeight;
int size = inform.biSizeImage;
int bitCount = inform.biBitCount;
int widthBytes = (width * bitCount + 31) / 32 * 4;
int npad = widthBytes - 3 * width;
data = new int[width*height];
int index = 0;
rgb = new byte[size];
file.read(rgb, 0, size);
for(int r = 0; r < height; r++){
for(int c = 0; c < width; c++){
data[c + width * (height - r - 1)] =
(((int)255 & 0xff) << 24) | (((int)rgb[index + 2] & 0xff) << 16) |
(((int)rgb[index + 1] & 0xff) << 8) | ((int)rgb[index] & 0xff);
index += 3;
}
index += npad;
}
Image image = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(width, height, data, 0, width));
file.close();
return image;
}
catch(Exception e){
e.printStackTrace();
System.out.println("Caught exception!");
}
return null;
}
public Image myWrite(Image image,String filePath){
try{
File file;
//if the filePath has no a postfix of ".bmp", add a ".bmp" to its tail
int pos = filePath.lastIndexOf(".bmp");
if (pos >= 0)
file = new File(filePath);
else
file = new File(filePath + ".bmp");
BufferedImage BImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D graph = BImage.createGraphics();
graph.drawImage(image, 0, 0, null);
graph.dispose();
ImageIO.write(BImage, "bmp", file);
return image;
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
return image;
}
public BitmapFileHeader getFileHeader(byte[] header){
BitmapFileHeader head = new BitmapFileHeader();
head.bfType[0] = (char)header[0];
head.bfType[1] = (char)header[1];
head.bfSize = toInteger(header, 2);
head.bfReserved = toInteger(header, 6);
head.bfOffBits = toInteger(header, 10);
return head;
}
public BitmapInfoHeader getInfoHeader(byte[] info){
BitmapInfoHeader information = new BitmapInfoHeader();
information.biSize = toInteger(info,0);
information.biWidth = toInteger(info,4);
information.biHeight = toInteger(info,8);
information.biPlanes = 1;
information.biBitCount = (short)info[14];
information.biCompression = toInteger(info,16);
information.biSizeImage = toInteger(info,20);
information.biXPelsPerMeter = toLong(info,24);
information.biYPelsPerMeter = toLong(info,28);
information.biClrUsed = toInteger(info,32);
information.biClrImportant = toInteger(info,36);
return information;
}
public int toInteger(byte[] data,int beg){
int ans;
ans = (((int)data[beg+3] & 0xff) << 24) | (((int)data[beg+2] & 0xff) << 16) |
(((int)data[beg+1] & 0xff) << 8) | ((int)data[beg] & 0xff);
return ans;
}
public long toLong(byte[] data,int beg){
long ans;
ans = (((long)data[beg+3] & 0xff) << 24) | (((long)data[beg+2] & 0xff) << 16) |
(((long)data[beg+1] & 0xff) << 8) | ((long)data[beg] & 0xff);
return ans;
}
public BitmapFileHeader getHeader(){
return head;
}
public BitmapInfoHeader getInfo(){
return inform;
}
public byte[] getRGB(){
return rgb;
}
public int[] getData(){
return data;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
GridWorld扩展任务1图像处理
共17个文件
class:10个
java:4个
project:1个
5星 · 超过95%的资源 需积分: 10 8 下载量 174 浏览量
2013-07-12
08:30:43
上传
评论
收藏 15KB ZIP 举报
温馨提示
中山大学暑期实训GridWorld项目扩展任务1图像处理:完成【开发】图像处理部分内容(TA现场检查程序运行,同时下午5:30前需提交程序代码到FTP上)
资源推荐
资源详情
资源评论
收起资源包目录
11331235_MaGuihong_DIP_CD.zip (17个子文件)
reader
.project 365B
src
IReader.java 234B
ReaderTest.java 3KB
imageProcessor.java 2KB
imageIO.java 4KB
.settings
org.eclipse.jdt.core.prefs 617B
.classpath 469B
bin
ReaderTest.class 4KB
imageProcessor$GreenFilter.class 631B
imageProcessor$BlueFilter.class 628B
imageIO$BitmapInfoHeader.class 651B
imageProcessor$RedFilter.class 625B
IReader.class 612B
imageIO$BitmapFileHeader.class 511B
imageProcessor$GrayFilter.class 740B
imageIO.class 5KB
imageProcessor.class 2KB
共 17 条
- 1
资源评论
- 幼稚园霸王2014-10-26帮助挺大的,自己动手比较好
- qq_151916832014-08-11很好,帮助很大,但有的地方显得繁琐!
GuihongMa
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功