用字节数组创建BMP文件

问题描述 投票:0回答:1

我的代码有问题,想法是创建黑色图像,然后进行颜色更改。目前我唯一想做的就是使用 ByteBuffer 类创建黑色图像,但是当我运行时图像已损坏。 这是代码

package co.edu.uptc.persistence;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class BMFile {
    private String fileName;
    private int width;
    private int heigth;
    private int imageSize;
    private int fileSize;

    private String fileType;
    private short reserved1;
    private short reserved2;
    private int dataBegin;
    private int headSize;
    private short layers;
    private short pixelSize;
    private int compresion;
    private int horizontalResolution;
    private int verticalResolution;
    private int colorTableSize;
    private int countMainColor;
    private int adjustment;

    public BMFile(String fileName, int width, int heigth) {
        this.fileName = fileName;

        this.width = width;
        this.heigth = heigth;
        imageSize = width * heigth * 3;
        fileSize = imageSize + 54;

        fileType = "BM";
        reserved1 = 0;
        reserved2 = 0;
        dataBegin = 54;
        headSize = 40;
        layers = 1;
        pixelSize = 24;
        compresion = 0;
        horizontalResolution = 0;
        verticalResolution = 0;
        colorTableSize = 0;
        countMainColor = 0;
    }

    public void saveFile() throws Exception {
        createFile(fileName, createHeader(), createImage());
    }

    private void createFile(String objectFile, byte[] header, byte[] image) throws Exception {
        try (FileOutputStream fuente = new FileOutputStream(objectFile)) {
            fuente.write(header);
            fuente.write(image);
        } catch (FileNotFoundException e) {
            System.out.println("eroooooorrrr");
            throw new Exception("Ha ocurrido un error archivo no encontrado");
        } catch (IOException e) {
            System.out.println("eroooooorrrr");
            throw new Exception("Ha ocurrido un error I/O");
        }
    }

    private byte[] createHeader() {
        byte[] header = new byte[54];
        
        header[0] = 'B';
        header[1] = 'M';
        ByteBuffer.wrap(header, 2, 4).putInt(fileSize);
        ByteBuffer.wrap(header, 10, 4).putInt(dataBegin);
        ByteBuffer.wrap(header, 14, 4).putInt(headSize);
        ByteBuffer.wrap(header, 18, 4).putInt(width);
        ByteBuffer.wrap(header, 22, 4).putInt(heigth);
        ByteBuffer.wrap(header, 26, 2).putShort(layers);
        ByteBuffer.wrap(header, 28, 2).putShort(pixelSize);
        ByteBuffer.wrap(header, 30, 4).putInt(compresion);
        ByteBuffer.wrap(header, 34, 4).putInt(imageSize);
        ByteBuffer.wrap(header, 38, 4).putInt(horizontalResolution);
        ByteBuffer.wrap(header, 42, 4).putInt(verticalResolution);
        ByteBuffer.wrap(header, 46, 4).putInt(colorTableSize);
        ByteBuffer.wrap(header, 50, 4).putInt(countMainColor);
        
        return header;
    }

    private byte[] createImage() {
        byte[] image = new byte[this.imageSize];
        for (int i = 0; i < image.length; i += 3) {
            image[i] = 0;
            image[i + 1] = 0;
            image[i + 2] = (byte) 0;
        }
        return image;
    }
    

当我查看图像属性时,我注意到图像大小很好,但其余部分由于某种原因失败了。

正如我之前所说,想法是创建一个黑色图像,然后修改 createImage 方法以设置其他颜色,但图像已损坏。

java image bmp bytebuffer
1个回答
0
投票

这有两个问题。第一个 bmp 应该是小尾数。第二你缺少一个标题。

这是我如何更改标题方法,以便它创建一个黑色的 bmp 文件。

private byte[] createHeader() {
    byte[] header = new byte[54];
    
    ByteBuffer buffer = ByteBuffer.wrap(header);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put((byte)'B');
    buffer.put((byte)'M');
    buffer.putInt(fileSize);
    buffer.putInt(0); //missing
    buffer.putInt(dataBegin);
    
    buffer.putInt(headSize);
    buffer.putInt(width);
    buffer.putInt(heigth);
    buffer.putShort(layers);
    buffer.putShort(pixelSize);
    buffer.putInt(compresion);
    buffer.putInt(0); // ignored
    buffer.putInt(horizontalResolution);
    buffer.putInt(verticalResolution);
    buffer.putInt(colorTableSize);
    buffer.putInt(countMainColor);
    
    return header;
}

我使用 this 作为 bmp 标题的参考。

© www.soinside.com 2019 - 2024. All rights reserved.