【问题标题】:How to import and export a raw image in Java?如何在 Java 中导入和导出原始图像?
【发布时间】:2019-02-08 00:49:38
【问题描述】:

正如标题所示,我正在尝试读取和输出原始图像文件。我在这个程序中的目标是读取原始图像并进行一些图像处理,然后输出最终图像。我知道如何执行此程序,但我一直试图在 java 中读取原始图像。我什至不确定如何将原始图像正确上传到我的项目文件夹中。任何建议将被认真考虑。到目前为止,这是我的代码(当然,我是从网站上获取此代码进行测试的)。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Assignment_1 {

void importImageIGuess() {

}

public static void main(String[] args) throws IOException {
    int width = 512;
    int length = 512;
    BufferedImage image = null;
    File f = null;

    //Read File I guess
    try {
        f = new File("lena.raw");
        image = new BufferedImage(width, length, BufferedImage.TYPE_INT_ARGB);
        image = ImageIO.read(f);
        System.out.println("Reading completed");

    }
    catch(IOException e) {
        System.out.println("Error " +e);

    }
    try{
        f = new File("lenaOutput.raw");
        ImageIO.write(image, "raw", f);
        System.out.println("Writing completed I guess");


    } catch (IOException e ) {
        System.out.println("Error " + e);
    }

}

}

【问题讨论】:

    标签: java image


    【解决方案1】:
    int[] rgb = image.getRGB(0, 0, width, height, null, 0, width);
    

    WritableRaster raster = image.getWritableRaster();
    

    诚然,getRGB 更简单。上面我假设 scan size 是宽度,有时它是对齐的。 RGBA、ARGB 和其中之一可能会有所不同。

    它是一个线性化的一维数组,可以使用IntBuffer,如果需要,甚至可以将字节顺序更改为小端。

    Path path = Paths.get("lena.raw");
    byte[] content = Files.readAllBytes(path);
    IntBuffer buf = ByteBuffer.wrap(content) /*.order(ByteOrder.LITTE_ENDIAN)*/ .asIntBuffer();
    int[] rgb = new int[content.length / 4];
    buf.get(rgb);
    BufferedImage outImage = new BufferedImage(width, length, BufferedImage.TYPE_INT_ARGB);
    outImage.setRGB(0, 0, width, height, rgb, 0, width);
    ImageIO.write(outImage, "png", new FileOutputStream(...));
    

    同时查看文件通道和 MappedByteBuffer 以获得速度和内存使用率。

    可能是第一次出现错误颜色 (TYPE_INT_ARGB)。

    【讨论】:

      【解决方案2】:

      ImageIO 支持 GIF、JPEG、PNG、BMP 和 WBMP,但您也可以在 ImageIO support for raw images (jrawio) 中找到一些用于 RAW 的库(例如 jrawio)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-01
        • 2010-09-09
        • 1970-01-01
        相关资源
        最近更新 更多