⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphkw.java

📁 java图形操作小软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		raf.write(b1);
		raf.write(b2);
		raf.write(b3);
		raf.write(b4);
	}
	private static boolean fileIsBMP(RandomAccessFile raf) {
		try {
			int i;

			raf.seek(0);
			if (raf.read()!='B') return false;
			if (raf.read()!='M') return false;

			System.out.println("0x02-0x05: "+readInt(raf, 2)+" file size");
			System.out.println("0x06-0x09: "+readInt(raf, 6)+" 0");
			System.out.println("0x0A-0x0D: "+readInt(raf, 0x0A)+ " | 54");
			System.out.println("0x0E-0x11: "+readInt(raf, 0x0E)+" | 40    图像描述信息块的大小");

			System.out.println("0x12-0x15: "+readInt(raf, 0x12)+" 图像宽度");
			System.out.println("0x16-0x19: "+readInt(raf, 0x16)+" 图像高度");
			System.out.println("0x1A-0x1B: "+raf.read()+", "+raf.read()+" | 1   plane总数");
			System.out.println("0x1C-0x1D: "+raf.read()+", "+raf.read()+" | 24   记录像素的位数");
			System.out.println("\n0x1E-0x21: "+readInt(raf, 0x1E)+" | 0   数据压缩方式");
			System.out.println("0x22-0x25: "+readInt(raf, 0x22)+" 图像区数据的大小");
			System.out.println("0x26-0x29: "+readInt(raf, 0x26)+" | 3780 水平每米有多少像素");
			System.out.println("0x2A-0x2D: "+readInt(raf, 0x2A)+" | 3780 垂直每米有多少像素");
			System.out.println("0x2E-0x31: "+readInt(raf, 0x2E)+" | 0    此图像所用的颜色数");
			System.out.println("0x32-0x36: "+readInt(raf, 0x32)+"        ??");

			i=readInt(raf, 0x1C); i=i<<16; i=i>>16; System.out.println("0x1C: "+i);
			if (i!=24) return false;

			i=readInt(raf, 0x1E); System.out.println("0x1E: "+i);
			if (i!=0) return false;

			return true;
		} catch (FileNotFoundException e) {
			System.err.println("FileNotFound:"+e);
		} catch (IOException e) {
			System.err.println("IO:"+e);
		}
		return true;
	}
	public static MemoryMap loadBMPFromFile(RandomAccessFile raf) {
		if (!fileIsBMP(raf)) return null;
		MemoryMap map=null;
		try {
			int height, width;
			int i, j;
			int r, g, b, c;
			int _spc;

			width=readInt(raf, 0x12);
			height=readInt(raf, 0x16);
			_spc=width%4;
			map=new MemoryMap(width, height);

			raf.seek(54);
			for (j=height-1; j>=0; j--) {
				for (i=0; i<width; i++) {
					b=raf.read();
					g=raf.read();
					r=raf.read();
					c=(r<<16)+(g<<8)+b;
					map.setRGB(i, j, c);
				}
				if (j>0)
					for (i=0; i<_spc; i++) raf.read();
			}

		} catch (IOException e) {
			System.err.println("IO:"+e);
		}
		return map;
	}
	public static void saveBMPToFile(RandomAccessFile raf, MemoryMap map) {
		int i, j;
		int width=map.getWidth();
		int height=map.getHeight();
		int _spc=width%4;
		int image_space=height*(width*3+_spc);
		int file_size=54+image_space;

		try {
			raf.seek(0);
			raf.write('B');
			raf.write('M');
			writeInt(raf, 2, file_size);		//file size
			writeInt(raf, 6, 0);			//reserve: 0
			writeInt(raf, 0x0A, 54);		//54
			writeInt(raf, 0x0E, 40);		//图像描述信息块的大小: 40
			writeInt(raf, 0x12, width);		//图像宽度
			writeInt(raf, 0x16, height);		//图像高度
			writeInt(raf, 0x1A, 1);			//plane总数: 1
			writeInt(raf, 0x1C, 24);		//记录像素的位数: 24
			writeInt(raf, 0x1E, 0);			//数据压缩方式:0
			writeInt(raf, 0x22, image_space);	//图像区数据的大小
			writeInt(raf, 0x26, 3780);		//水平每米有多少像素: 3780
			writeInt(raf, 0x2A, 3780);		//垂直每米有多少像素: 3780
			writeInt(raf, 0x2E, 0);			//此图像所用的颜色数: 0
			writeInt(raf, 0x32, 0);			//??: 0

			raf.seek(54);
			for (j=height-1; j>=0; j--) {
				for (i=0; i<width; i++) {
					raf.write(map.getBlue(i, j));
					raf.write(map.getGreen(i, j));
					raf.write(map.getRed(i, j));
				}
				if (j>0)
					for (i=0; i<_spc; i++) raf.write(0);
			}

		} catch (IOException e) {
			System.err.println("IO:"+e);
		}
	}
}

//MemoryMap===================================================

class MemoryMap extends BufferedImage {
	MemoryMap(int x, int y) {
		super(x, y, TYPE_INT_BGR);
	}
	public int getRed(int x, int y) {
		return (getRGB(x, y)>>16) & 255;
	}
	public int getGreen(int x, int y) {
		return (getRGB(x, y)>>8) & 255;
	}
	public int getBlue(int x, int y) {
		return getRGB(x, y) & 255;
	}
	public MemoryMap doFANSE() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp=new MemoryMap(w, h);
		for (i=0; i<w; i++)
			for (j=0; j<h; j++)
				tmp.setRGB(i, j, (~getRGB(i, j)) & (65536*256-1));
		return tmp;
	}
	public MemoryMap doJUNHENG() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();
		int r_max=0, r_min=255, g_max=0, g_min=255, b_max=0, b_min=255;
		int r, g, b, c;
		float k1, k2, k3;

		MemoryMap tmp=new MemoryMap(w, h);
		for (i=0; i<w; i++)
			for (j=0; j<h; j++) {
				r=getRed(i, j);
				g=getGreen(i, j);
				b=getBlue(i, j);
				if (r>r_max) r_max=r;
				if (r<r_min) r_min=r;
				if (g>g_max) g_max=g;
				if (g<g_min) g_min=g;
				if (b>b_max) b_max=b;
				if (b<b_min) b_min=b;
			}
		k1=256/(r_max-r_min+1);
		k2=256/(g_max-g_min+1);
		k3=256/(b_max-b_min+1);
		for (i=0; i<w; i++)
			for (j=0; j<h; j++) {
				if (k1!=0) r=(int)((getRed(i, j)-r_min)*k3); else r=getRed(i, j);
				if (k2!=0) g=(int)((getGreen(i, j)-g_min)*k3); else g=getGreen(i, j);
				if (k3!=0) b=(int)((getBlue(i, j)-b_min)*k3); else b=getBlue(i, j);
				c=((r<<16)+(g<<8)+b) & (65536*256-1);
				tmp.setRGB(i, j, c);
			}
		return tmp;
	}
	public MemoryMap doSHUIPING() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp=new MemoryMap(w, h);
		for (i=0; i<w; i++)
			for (j=0; j<h; j++)
				tmp.setRGB(w-i-1, j, getRGB(i, j));
		return tmp;
	}
	public MemoryMap doCHUIZHI() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp=new MemoryMap(w, h);
		for (i=0; i<w; i++)
			for (j=0; j<h; j++)
				tmp.setRGB(i, h-j-1, getRGB(i, j));
		return tmp;
	}
	public MemoryMap doXIANXINGPINGHUA() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp=new MemoryMap(w, h);
		G_XX filter=new G_XX();
		filter.start(this);
		for (j=0; j<h; j++)
			for (i=0; i<w; i++)
				tmp.setRGB(i, j, filter.getNextValue());
		return tmp;
	}
	public MemoryMap doZHONGZHI() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp=new MemoryMap(w, h);
		G_ZZ filter=new G_ZZ();
		filter.start(this);
		for (j=0; j<h; j++)
			for (i=0; i<w; i++)
				tmp.setRGB(i, j, filter.getNextValue());
		return tmp;
	}
	public MemoryMap doXIANXINGRUIHUA() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp=new MemoryMap(w, h);
		G_BY filter=new G_BY();
		filter.start(this);
		for (j=0; j<h; j++)
			for (i=0; i<w; i++)
				tmp.setRGB(i, j, filter.getNextValue());
		return tmp;
	}

	public MemoryMap doHENGXIANGMOHU() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp=new MemoryMap(w, h);
		X_MOHU filter=new X_MOHU();
		filter.start(this);
		for (j=0; j<h; j++)
			for (i=0; i<w; i++)
				tmp.setRGB(i, j, filter.getNextValue());
		return tmp;
	}


	public MemoryMap doSHUXIANGMOHU() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp=new MemoryMap(w, h);
		Y_MOHU filter=new Y_MOHU();
		filter.start(this);
		for (j=0; j<h; j++)
			for (i=0; i<w; i++)
				tmp.setRGB(i, j, filter.getNextValue());
		return tmp;
	}

	public MemoryMap doFUDIAOXIAOGUO() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp=new MemoryMap(w, h);
		FUDIAO filter=new FUDIAO();
		filter.start(this);
		for (j=0; j<h; j++)
			for (i=0; i<w; i++)
				tmp.setRGB(i, j, filter.getNextValue());
		return tmp;
	}


	public MemoryMap doZONGTIMOHU() {
		int i, j;
		int w=getWidth(),
		    h=getHeight();

		MemoryMap tmp1=new MemoryMap(w, h);
		X_MOHU filter_x=new X_MOHU();
		filter_x.start(this);
		for (j=0; j<h; j++)
			for (i=0; i<w; i++)
				tmp1.setRGB(i, j, filter_x.getNextValue());

		MemoryMap tmp=new MemoryMap(w, h);
		Y_MOHU filter_y=new Y_MOHU();
		filter_y.start(tmp1);
		for (j=0; j<h; j++)
			for (i=0; i<w; i++)
				tmp.setRGB(i, j, filter_y.getNextValue());
		return tmp;
	}


}

//BMPFile===================================================

class BMPFile {
	private RandomAccessFile file;
	private File _file;
	private MemoryMap map;
	private MemoryMap last_map;
	public boolean load(String filename) {
		try {
			if (file!=null) file.close();
			_file=new File(filename);
			file=new RandomAccessFile(_file, "rw");
		} catch (FileNotFoundException e) {
			System.err.println("FileNotFound:"+e);
			return false;
		} catch (IOException e) {return false;}

		map=BMPFileAccessor.loadBMPFromFile(file);

		if (map==null) return false;

		return true;
	}
	public boolean save() {
		BMPFileAccessor.saveBMPToFile(file, map);
		return true;
	}
	public boolean saveAs(String filename) {
		try {
			if (file==null) return false;
			file.close();
			File _file=new File(filename);
			file=new RandomAccessFile(_file, "rw");
		} catch (FileNotFoundException e) {
			System.err.println("FileNotFound:"+e);
			return false;
		} catch (IOException e) {return false;}

		BMPFileAccessor.saveBMPToFile(file, map);

		return true;
	}
	public boolean doFileter(String opt) {
		last_map=map;
		if (opt.equals("FAN_SE")) map=map.doFANSE();
		if (opt.equals("JUN_HENG")) map=map.doJUNHENG();
		if (opt.equals("SHUI_PING")) map=map.doSHUIPING();
		if (opt.equals("CHUI_ZHI")) map=map.doCHUIZHI();
		if (opt.equals("XIAN_XING_PING_HUA")) map=map.doXIANXINGPINGHUA();
		if (opt.equals("ZHONG_ZHI")) map=map.doZHONGZHI();
		if (opt.equals("XIAN_XING_RUI_HUA")) map=map.doXIANXINGRUIHUA();
		if (opt.equals("HENG_XIANG_MO_HU")) map=map.doHENGXIANGMOHU();
		if (opt.equals("SHU_XIANG_MO_HU")) map=map.doSHUXIANGMOHU();
		if (opt.equals("ZONG_TI_MO_HU")) map=map.doZONGTIMOHU();
		if (opt.equals("FU_DIAO_XIAO_GUO")) map=map.doFUDIAOXIAOGUO();
		return true;
	}
	public boolean undo() {
		MemoryMap tmp=map;
		map=last_map;
		last_map=tmp;
		return true;
	}

	public MemoryMap getMemoryMap() {
		return map;
	}

	public Dimension getSize() {
		if (map==null) return new Dimension(0, 0);
		return new Dimension(map.getWidth(), map.getHeight());
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -