程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 自己寫了一個imagebuffer 轉 bmp 流的代碼段

自己寫了一個imagebuffer 轉 bmp 流的代碼段

編輯:關於JAVA

try {

BufferedImage bi = null;

if (isURL == true) {

bi = ImageIO.read(new URL(inFileName));

}

else {

bi = ImageIO.read(new File(inFileName));

}

int[] rgbArray = new int[101 * 29];//我測試用的圖象文件象素值寬度是101寬,29長的

bi.getRGB(0, 0, 101, 29, rgbArray, 0, 101);

for (int i = 0; i < 101 * 29; i++) {//我做的只是雙色圖,所以這裡查出每一個象素是1(黑)還是0(白)

if ((rgbArray[i] & 0xffffff) == 0x000000)

rgbArray[i] = 0;

else

rgbArray[i] = 1;

}

//以下是BMP文件格式的定義,更加具體的說明可以到http://www.wotsit.org找

int[] identifIEr = { 'B', 'M' };

int[] fileSize = { 0x0E, 0x02, 0x00, 0x00 };

int[] reserved = { 0x00, 0x00, 0x00, 0x00 };

int[] bitmapDataOffset = { 0x3E, 0x00, 0x00, 0x00 };

int[] bitmapHeaderSize = { 0x28, 0x00, 0x00, 0x00 };

int[] width = { 0x65, 0x00, 0x00, 0x00 };

int[] height = { 0x1D, 0x00, 0x00, 0x00 };

int[] planes = { 0x01, 0x00 };

int[] bitsPerPixel = { 0x01, 0x00 };

int[] compression = { 0x00, 0x00, 0x00, 0x00 };

int[] bitmapDataSize = { 0xD0, 0x01, 0x00, 0x00 };

int[] hResolution = { 0x00, 0x00, 0x00, 0x00 };

int[] vResolution = { 0x00, 0x00, 0x00, 0x00 };

int[] colors = { 0x00, 0x00, 0x00, 0x00 };

int[] importantColors = { 0x00, 0x00, 0x00, 0x00 };

int[] palette = { 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00 };

//記住bmp沒一個掃描行都要擴充到下一個4bytes的倍數,如101就要變成128,擴充的byte補0即可

int[] bitmapData = new int[(101 + 27) * 29 / 8];

int n = 0;

for (int i = 0; i < 29; i++) { //BMP格式是從最後一行開始scan的

for (int j = 1; j <= 101; j++) {

if (rgbArray[(28 - i) * 101 + j - 1] == 1) {

bitmapData[n] = bitmapData[n] | (1 << (8 - j % 8) % 8);

}

if (j % 8 == 0) {

n++;

}

if (j == 101) {

n += 4;

}

}

}

RandoMaccessFile o = new RandomAccessFile(outFileName, "rw");

o.setLength(0);

for (int i = 0; i < identifIEr.length; i++) {

o.write(identifIEr[i]);

}

for (int i = 0; i < fileSize.length; i++) {

o.write(fileSize[i]);

}

for (int i = 0; i < reserved.length; i++) {

o.write(reserved[i]);

}

for (int i = 0; i < bitmapDataOffset.length; i++) {

o.write(bitmapDataOffset[i]);

}

for (int i = 0; i < bitmapHeaderSize.length; i++) {

o.write(bitmapHeaderSize[i]);

}

for (int i = 0; i < width.length; i++) {

o.write(width[i]);

}

for (int i = 0; i < height.length; i++) {

o.write(height[i]);

}

for (int i = 0; i < planes.length; i++) {

o.write(planes[i]);

}

for (int i = 0; i < bitsPerPixel.length; i++) {

o.write(bitsPerPixel[i]);

}

for (int i = 0; i < compression.length; i++) {

o.write(compression[i]);

}

for (int i = 0; i < bitmapDataSize.length; i++) {

o.write(bitmapDataSize[i]);

}

for (int i = 0; i < hResolution.length; i++) {

o.write(hResolution[i]);

}

for (int i = 0; i < vResolution.length; i++) {

o.write(vResolution[i]);

}

for (int i = 0; i < colors.length; i++) {

o.write(colors[i]);

}

for (int i = 0; i < importantColors.length; i++) {

o.write(importantColors[i]);

}

for (int i = 0; i < palette.length; i++) {

o.write(palette[i]);

}

for (int i = 0; i < bitmapData.length; i++) {

o.write(bitmapData[i]);

}

o.close();

}

catch (MalformedURLException e) {

e.printStackTrace();

}

catch (IOException e) {

e.printStackTrace();

}

如果有什麼不明白的,請聯系我[email protected],我將盡我所能的幫助你。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved