【问题标题】:Writing a byte array to a bmp java将字节数组写入 bmp java
【发布时间】:2015-07-05 07:38:26
【问题描述】:

正如标题所示,我正在尝试将字节数组写入 Java 中的 bmp 文件。目前,我的程序成功地将数据写入文件位置,但是它似乎缺少数据并且因此无法打开。下面列出的两个函数的目标是:

fromImage 获取灰度 bmp 图像字节数据并将每个整数转换为二进制字符串,然后将其存储在 LinkedList 节点中。 toImage 采用 LinkedList,将二进制字符串转换回整数,然后将新的字节数组写回另一个文件。

public static LinkedList<String> fromImage(BufferedImage img) {
    LinkedList<String> new_buff = new LinkedList<String>();
    //try{
        //img = ImageIO.read(new File("img/lena.bmp"));
        byte[] byte_buffer = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();

        for(byte b : byte_buffer){
            String buffer;
            buffer = Integer.toBinaryString(b & 255 | 256).substring(1);
            new_buff.addLast(buffer);
            //System.out.println(buffer);
        }
    //}catch(IOException e){}
    System.out.println("Exiting fromImage");
    return new_buff;
}

// Save a binary number as a BMP image
// Image input hardcoded atm
public static BufferedImage toImage(LinkedList<String> bi) {
    BufferedImage img = null;
    int b;
    byte[] bytes = new byte[bi.size()];
    for(int i = 0; i < bi.size(); i++){
        String temp = bi.get(i);
        b = Integer.parseInt(temp);
        bytes[i] = (byte) b;
        //System.out.println(i);
    }
    System.out.println("Exiting For loop");
    try{
        Files.write(Paths.get("img/encrypted.bmp"), bytes);
        //img = ImageIO.read(new File("img/lena.bmp"));
        //ImageIO.write(img, "bmp", new File("img/encrypted.bmp"));
        //img = ImageIO.read(new File("img/encrypted.bmp"));
    }catch(IOException e){}
    System.out.println("Exiting toImage");
    return img;
}

所以最终,我的问题是 - 我丢失的数据在哪里,为什么会丢失,我可以做些什么来修复它?

【问题讨论】:

  • 通过LinkedList&lt;String&gt; 进行的转换是关于什么的?您能否对这真的有效进行单元测试(即可以正确往返byte[])?
  • 我不是 100% 确定我明白你在问什么,但我将它存储为 LinkedList 的原因是因为从原始图像中提取的像素字节是整数和我需要对二进制数进行最低有效位更改。

标签: java arrays image bytearray


【解决方案1】:

我只想把Ekleog's answer变成一个可用的代码:

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
public static boolean arrayToBMP(byte[] pixelData, int width, int height, File outputFile) throws IOException {
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    img.setData(Raster.createRaster(img.getSampleModel(), new DataBufferByte(pixelData, pixelData.length), null));
    return javax.imageio.ImageIO.write(img, "bmp", outputFile);
}

【讨论】:

    【解决方案2】:

    BMP 有一个file structure

    这里,你正在写入一个名为“encrypted.bmp”的文件,所以我想你的字节是加密的东西,因此不代表一个有效的 bmp 文件。

    您必须遵守 BMP 文件结构,添加页眉和页脚,以便您的字节是例如。 BMP 文件的像素部分。

    最简单的方法是将图像写入BufferedImage img,然后使用ImageIO.write(img, "BMP", new File("encrypted.bmp"))

    【讨论】:

    • 好的。这是一个朋友建议的。您对我可以在哪里学习如何添加缺少的页眉/页脚信息有什么建议吗?目前,没有加密信息。加密将是像素二进制数据上最不重要的位更改,但是我还没有实现那部分。
    • 维基百科链接具有完整的文件结构,因此您可以使用它来编写您的位图。否则,this site 似乎很清楚如何使用 BMP 文件格式。如果您不想重新发明轮子,只需使用ImageIO.write(img, "BMP", new File("encrypted.bmp")),其中img 是带有像素值的BufferedImage
    • 我之前在使用 ImageIO 并且遇到了问题,但是现在我再次查看它时,看起来我可能一直在错误地使用它。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 2014-04-10
    相关资源
    最近更新 更多