ขอคำปรึกษาเกี่บว เขียน ไฟล์bitmapในโค้ดนี้ค่ะ
จากโค้ดข้างนะค่ะ หนูทำการกำหนดลงสีทีละพิกเซลแบบสุ่ม มันมีปัญหาตอนเวลาเซฟนะค่่ะ ถ้าWIDTHกับHEIGHT เท่ากันภาพที่ออกมาจะไม่มีปัญหา แต่ถ้าWIDTHกับHEIGHTไม่เท่ากัน ภาพที่เซฟออกมาพิกเซลจะแตกค่ะ ควรจะแก้ไขยังไงดี ค่ะ หรือคิดว่าตรงจุดไหนดีค่ะ ขอบคุณค่ะ import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BMP {
private final static int BMP_CODE = 19778;//19778; private static final String filename = "MyImage12.bmp"; private static final int WIDTH = 250; private static final int HEIGHT = 200; static int[][] rgbValues=new int [WIDTH][HEIGHT];
byte [] bytes; public static void main(String[] args) { int x = (int) (Math.random() * WIDTH); int y =(int) (Math.random() * HEIGHT); int r = ( int ) ( Math.random() * 256 ); int b = ( int ) ( Math.random() * 256 ); int g =( int ) ( Math.random() * 256 ); int alpha=( int ) ( Math.random() * 256 ); rgbValues[x][y]=r|g<<8|b<<16; System.out.println("[x][y]="+x+","+y); //System.out.println("rgb="+r+","+b+","+g);
int x_m=x; int y_m=y; //System.out.println("[xm1][ym1]="+x_m+","+y_m); for(int q=1;q<100;q++){ //System.out.println("[xm1][ym1]="+l+","+x+","+y); int nx=x-q; int ny=y-q; rgbValues[nx][ny]=r|g<<8|b<<16; System.out.println(nx+","+ny); } new BMP().saveBMP(filename,rgbValues);
//int red, green, blue;
}
public void saveBMP(String filename, int [][] rgbValues){
try {
FileOutputStream fos = new FileOutputStream(new File(filename));
bytes = new byte[54 + 3*rgbValues.length*rgbValues[0].length + getPadding(rgbValues[0].length)*rgbValues.length];
saveFileHeader();
saveInfoHeader(rgbValues.length, rgbValues[0].length);
saveRgbQuad();
saveBitmapData(rgbValues);
fos.write(bytes);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
private void saveFileHeader() {
byte[]a=intToByteCouple(BMP_CODE);
bytes[0]=a[1];
bytes[1]=a[0];
a=intToFourBytes(bytes.length);
bytes[5]=a[0];
bytes[4]=a[1];
bytes[3]=a[2];
bytes[2]=a[3];
//data offset
bytes[10]=54;
}
private void saveInfoHeader(int height, int width) {
bytes[14]=40;
byte[]a=intToFourBytes(width);
bytes[22]=a[3];
bytes[23]=a[2];
bytes[24]=a[1];
bytes[25]=a[0];
a=intToFourBytes(height);
bytes[18]=a[3];
bytes[19]=a[2];
bytes[20]=a[1];
bytes[21]=a[0];
bytes[26]=1;
bytes[28]=24;
}
private void saveRgbQuad() {
}
private void saveBitmapData(int[][]rgbValues) {
int i;
for(i=0;i<rgbValues.length;i++){
writeLine(i, rgbValues);
}
}
private void writeLine(int row, int [][] rgbValues) {
final int offset=54;
final int rowLength=rgbValues[row].length;
final int padding = getPadding(rgbValues[0].length);
int i;
for(i=0;i<rowLength;i++){
int rgb=rgbValues[row][i];
int temp=offset + 3*(i+rowLength*row) + row*padding;
bytes[temp] = (byte) (rgb>>16);
bytes[temp +1] = (byte) (rgb>>8);
bytes[temp +2] = (byte) rgb;
}
i--;
int temp=offset + 3*(i+rowLength*row) + row*padding+3;
for(int j=0;j<padding;j++)
bytes[temp +j]=0;
}
private byte[] intToByteCouple(int x){
byte [] array = new byte[2];
array[1]=(byte) x;
array[0]=(byte) (x>>8);
return array;
}
private byte[] intToFourBytes(int x){
byte [] array = new byte[4];
array[3]=(byte) x;
array[2]=(byte) (x>>8);
array[1]=(byte) (x>>16);
array[0]=(byte) (x>>24);
return array;
}
private int getPadding(int rowLength){
int padding = (3*rowLength)%4;
if(padding!=0)
padding=4-padding;
return padding;
}
}
|