Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中创建位图时出错_Java_Bitmap_Drawing_Bmp - Fatal编程技术网

在Java中创建位图时出错

在Java中创建位图时出错,java,bitmap,drawing,bmp,Java,Bitmap,Drawing,Bmp,我正在尝试从像素值的2d数组创建位图。我在网上找到了一些代码(摘自): 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.io.FileOutputStream; 导入java.io.IOException; 公共类BMP{ 专用最终静态int BMP_代码=19778; 字节[]字节; public void saveBMP(字符串文件名,int[][]rgbValue){ 试一试{ FileOutputStream fos=新的F

我正在尝试从像素值的2d数组创建位图。我在网上找到了一些代码(摘自):

导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
公共类BMP{
专用最终静态int BMP_代码=19778;
字节[]字节;
public void saveBMP(字符串文件名,int[][]rgbValue){
试一试{
FileOutputStream fos=新的FileOutputStream(新文件(文件名));
bytes=新字节[54+3*rgbValues.length*rgbValues[0]。length+getPadding(rgbValues[0]。length)*rgbValues.length];
saveFileHeader();
saveInfoHeader(rgbValue.length,rgbValue[0].length);
saveRgbQuad();
saveBitmapData(rgbValue);
fos.写入(字节);
fos.close();
}catch(filenotfounde异常){
}捕获(IOE异常){
}
}
私有void saveFileHeader(){
字节[]a=IntToByteCourt(BMP_代码);
字节[0]=a[1];
字节[1]=a[0];
a=四字节(字节.长度);
字节[5]=a[0];
字节[4]=a[1];
字节[3]=a[2];
字节[2]=a[3];
//数据偏移量
字节[10]=54;
}
私有void saveInfoHeader(整数高度、整数宽度){
字节[14]=40;
字节[]a=四个字节(宽度);
字节[22]=a[3];
字节[23]=a[2];
字节[24]=a[1];
字节[25]=a[0];
a=四个字节(高度);
字节[18]=a[3];
字节[19]=a[2];
字节[20]=a[1];
字节[21]=a[0];
字节[26]=1;
字节[28]=24;
}
私有void saveRgbQuad(){
}
私有void saveBitmapData(int[][]rgbValue){
int i;
对于(i=0;i16);
字节[temp+1]=(字节)(rgb>>8);
字节[temp+2]=(字节)rgb;
}
我--;
int temp=偏移量+3*(i+行长*行)+行*填充+3;
对于(int j=0;j>8);
返回数组;
}
专用字节[]intToFourBytes(intx){
字节[]数组=新字节[4];
数组[3]=(字节)x;
数组[2]=(字节)(x>>8);
数组[1]=(字节)(x>>16);
数组[0]=(字节)(x>>24);
返回数组;
}
私有int getPadding(int行长度){
整数填充=(3*行长度)%4;
如果(填充!=0)
填充=4-填充;
返回填充;
}
}
现在,我尝试使用以下代码创建大小为10x5的位图:

int[][] bmpData = new int[10][5];
for(int x = 0; x < 10; x++) {
    for(int y = 0; y < 5; y++) {
        bmpData[x][y] = 127 | 127 << 8 | 127 << 16;
    }
}
new BMP().saveBMP("test.bmp", bmpData);
int[]bmpData=newint[10][5];
对于(int x=0;x<10;x++){
对于(int y=0;y<5;y++){

bmpData[x][y]=127 | 127在我看来,该代码并没有按预期工作

查看此部分,例如:

saveInfoHeader(rgbValues.length, rgbValues[0].length);
saveInfoHeader
具有以下签名:

saveInfoHeader(int height, int width)
因此,当你通过
int[100][50];
时,你应该得到高度为100、宽度为50的bmp

我想这部分代码是不正确的:

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];
根据BMP标准,宽度应该在高度之前声明,这里的宽度是较早处理的,但是(我想,由于编辑错误)它是以更大的偏移量写入的

如果将此更改为:

byte[]a=intToFourBytes(width);
bytes[18]=a[3];
bytes[19]=a[2];
bytes[20]=a[1];
bytes[21]=a[0];

a=intToFourBytes(height);
bytes[22]=a[3];
bytes[23]=a[2];
bytes[24]=a[1];
bytes[25]=a[0];
,您将得到以下结果:


您得到的模式是由于高度/宽度不匹配导致BMP填充计算错误的结果。

bmpData
显然是一致的,因此,基本上您要求stackoverflow调试某人的
BMP
类。我不确定,但可能您需要将其发布在此处。是的,BMP类存在著名的x/y与行/列混淆。这是确实是这个问题!我现在得到了一个我期望的位图。谢谢你的回答。
byte[]a=intToFourBytes(width);
bytes[18]=a[3];
bytes[19]=a[2];
bytes[20]=a[1];
bytes[21]=a[0];

a=intToFourBytes(height);
bytes[22]=a[3];
bytes[23]=a[2];
bytes[24]=a[1];
bytes[25]=a[0];