`
Cb123456
  • 浏览: 63490 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

保存成bmp格式

    博客分类:
  • java
阅读更多

 此Demo用来说明保存成Bmp格式

 步骤1:以以前做的画图板为例,点击"保存按钮后",保存到"E:\\test3.bmp"

 运行效果:

 
 

 源码:

 

	private Robot robot;

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String actionCommand = e.getActionCommand();
			// System.out.println("得到的ActionCommand"+actionCommand);
			if(actionCommand.equals("保存")){
				try {
					robot = new Robot();
				} catch (AWTException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				Rectangle screenRect = new Rectangle(drawpanelX,drawpanelY,drawpanelWidth,drawpanelHeight);
				BufferedImage img =robot.createScreenCapture(screenRect);
				
				BMPWriter bmp = new BMPWriter();
	            //IndexColorModel mf = new IndexColorModel(8, 16, r,  g, b);
				
				File out = new File("E:\\test3.bmp");
				bmp.write(img, out);
				
				
				
			}

		}

 

 

 

  步骤2 :BMPWriter,用来保存成BMP

 

import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class BMPWriter {

	public static void write(BufferedImage img, File out) {

		int width = img.getWidth();
		int tripleWidth = width * 3;
		int height = img.getHeight();
		int fullTriWidth = tripleWidth % 4 == 0 ? tripleWidth
				: 4 * ((tripleWidth / 4) + 1);
		int[] px = new int[width * height];
		px = img.getRGB(0, 0, width, height, px, 0, width);
		byte[] rgbs = new byte[tripleWidth * height];
		int index = 0;
		// r, g, b数组
		for (int i = height - 1; i >= 0; i--) {
			for (int j = 0; j < width; j++) {
				int pixel = px[i * width + j];
				rgbs[index++] = (byte) pixel;
				rgbs[index++] = (byte) (pixel >>> 8);
				rgbs[index++] = (byte) (pixel >>> 16);
			}
		}
		// 补齐扫描行长度为4的倍数
		byte[] fullrgbs = new byte[fullTriWidth * height];
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < fullTriWidth; j++) {
				if (j < tripleWidth) {
					fullrgbs[fullTriWidth * i + j] = rgbs[i * tripleWidth + j];
				} else {
					fullrgbs[fullTriWidth * i + j] = 0;
				}
			}
		}

		index = 0;

		int fheader = 14;
		int infoheader = 40;
		int board = 0;
		int offset = fheader + infoheader + board;
		int length = width * height * 3 + offset;
		short frame = 1;
		short deep = 24;
		int fbl = 3800;
		DataOutputStream dos = null;
		try {
			FileOutputStream fos = new FileOutputStream(out);
			dos = new DataOutputStream(fos);
			dos.write('B');
			dos.write('M');// 1格式头
			wInt(dos, length);// 2-3文件大小
			wInt(dos, 0);// 4-5保留
			wInt(dos, offset);// 6-7偏移量
			wInt(dos, infoheader);// 8-9头信息
			wInt(dos, width);// 10-11宽
			wInt(dos, height);// 12-13高
			wShort(dos, frame);// 14 = 1帧数
			wShort(dos, deep);// 15 = 24位数
			wInt(dos, 0);// 16-17压缩
			wInt(dos, 4);// 18-19 size
			wInt(dos, fbl);// 20-21水平分辨率
			wInt(dos, fbl);// 22-23垂直分辨率
			wInt(dos, 0);// 24-25颜色索引 0为所有
			wInt(dos, 0);// 26-27重要颜色索引 0为所有
			// wInt(0);//28-35
			// wInt(0);
			// wInt(0);
			// wInt(0);//28-35彩色板
			dos.write(fullrgbs);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				dos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private static void wInt(DataOutputStream dos, int i) throws IOException {
		dos.write(i);
		dos.write(i >> 8);
		dos.write(i >> 16);
		dos.write(i >> 24);
	}

	private static void wShort(DataOutputStream dos, short i)
			throws IOException {
		dos.write(i);
		dos.write(i >> 8);
	}

}

  

 

  总结:BMPWriter这个类网上很多的,写法也大同小异,我见的一般就是传个Color[][],然而,当初怎么也搞不出了,最近突然脑子闪了一下,用步骤一,得到BufferedImage,里面就有Color[][],就能很好实现存储为BMP了. 

  • 大小: 5.2 MB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics