简体   繁体   English

读取.bmp映像,并从映像的第10个字节中减去10,然后用Java重新创建映像

[英]Read .bmp image and subtract 10 from 10th byte of the image and re-create the image in Java

I am creating an application which will read image byte/pixel/data from an .bmp image and store it in an byte/char/int/etc array. 我正在创建一个应用程序,该程序将从.bmp图像读取图像字节/像素/数据,并将其存储在字节/字符/整数/等数组中。

Now, from this array, I want to subtract 10 (in decimal) from the data stored in the 10th index of an array. 现在,从该数组中,我想从存储在数组的第10个索引中的数据中减去10(十进制)。

I am able to successfully store the image information in the array created. 我能够成功将图像信息存储在创建的数组中。 But when I try to write the array information back to .bmp image, the image created is not viewable. 但是,当我尝试将数组信息写回到.bmp映像时,创建的映像不可见。

This is the piece of code which I tried to do so. 这是我尝试执行的代码。

In this code, I am not subtracting 10 from the 10th index of an array . 在这段代码中,我没有从array的第10个索引中减去10

public class Test1 {

    public static void main(String[] args) throws IOException{
        File inputFile = new File("d://test.bmp");
        FileReader inputStream = new FileReader("d://test.bmp");
        FileOutputStream outputStream = new FileOutputStream("d://test1.bmp");
        /*
         * Create byte array large enough to hold the content of the file.
         * Use File.length to determine size of the file in bytes.
         */
        char fileContent[] = new char[(int)inputFile.length()];


        for(int i = 0; i < (int)inputFile.length(); i++){
            fileContent[i] = (char) inputStream.read();
        }

        for(int i = 0; i < (int)inputFile.length(); i++){
            outputStream.write(fileContent[i]);
        }
    }
}

Instead of char[], use byte[] 代替char [],使用byte []

Here's a modified version if your code which works: 如果您的代码有效,这是修改后的版本:

public class Test {

public static void main(String[] args) throws IOException {
    File inputFile = new File("someinputfile.bmp");
    FileOutputStream outputStream = new FileOutputStream("outputfile.bmp");
    /*
     * Create byte array large enough to hold the content of the file.
     * Use File.length to determine size of the file in bytes.
     */
    byte fileContent[] = new byte[(int)inputFile.length()];


    new FileInputStream(inputFile).read(fileContent);

    for(int i = 0; i < (int)inputFile.length(); i++){
        outputStream.write(fileContent[i]);
    }

    outputStream.close();
 }
}

To make your existing code work you should replace the FileReader with a FileInputStream. 要使现有代码正常工作,应将FileReader替换为FileInputStream。 According to the FileReader javadoc: 根据FileReader javadoc:

FileReader is meant for reading streams of characters. FileReader用于读取字符流。 For reading streams of raw bytes, consider using a FileInputStream. 为了读取原始字节流,请考虑使用FileInputStream。

Modifying your sample as below 如下修改样本

  public static void main(String[] args) throws IOException
 {
File inputFile = new File("d://test.bmp");
FileInputStream inputStream = new FileInputStream("d://test.bmp");
FileOutputStream outputStream = new FileOutputStream("d://test1.bmp");
/*
 * Create byte array large enough to hold the content of the file.
 * Use File.length to determine size of the file in bytes.
 */
byte fileContent[] = new byte[(int)inputFile.length()];

for(int i = 0; i < (int)inputFile.length(); i++){
    fileContent[i] =  (byte) inputStream.read();
}

inputStream.close();
for(int i = 0; i < (int)inputFile.length(); i++){
    outputStream.write(fileContent[i]);
}

outputStream.flush();
outputStream.close();

} }

This work for me to create a copy of the original image. 这对我来说可以创建原始图像的副本。

Though as mentioned in the comments above this is probably not the correct approach for what you are trying to achieve. 尽管如上面的评论中所述,这可能不是您要实现的正确方法。

Other have pointed you at errors in your code (using char instead of byte mostly), however, even if you fix that, you probably will end up with a non-loadable image if you change the value of the 10th byte in the file. 其他人则指出了您的代码错误(大多数情况下使用char而不是byte ),但是,即使您解决了该错误,如果您更改文件中第10个字节的值,也可能会导致图像无法加载。

This is because, a .bmp image file starts with an header containing information about the file (color depth, dimensions, ... see BMP file format ) before any actual image data. 这是因为.bmp图像文件以任何实际图像数据之前的标题开头,该标题包含有关该文件的信息(颜色深度,尺寸等,请参阅BMP文件格式 )。 Specifically, the 10th byte is part of a 4 byte integer storing the offset of the actual image data (pixel array). 具体地说,第10个字节是4字节整数的一部分,该整数存储实际图像数据(像素阵列)的偏移量。 So subtracting 10 from this value will probably make the offset pointing at the wrong point in the file, and your image loader doing bound checking will probably consider this invalid. 因此,从该值中减去10可能会使偏移量指向文件中的错误点,并且您的图像加载器在进行边界检查时可能会认为此无效。

What you really want to do is load the image as an image and manipulate the pixel values directly. 您真正想要做的是将图像作为图像加载并直接操纵像素值。 Something like that: 像这样:

BufferedImage originalImage = ImageIO.read(new File("d://test.bmp"));
int rgb = originalImage.getRGB(10, 0);
originalImage.setRGB(rgb >= 10 ? rgb - 10 : 0);
ImageIO.write(originalImage, "bmp", new File("d://test1.bmp"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM