简体   繁体   中英

how to fill a raw file with an image buffer, using an alternative to ImageIO.write

The problem is that ImageIO.write does not recognize the raw format, so the file does not fill it. I tried the same code with png and it created it correctly. Is there some alternative to ImageIO.write or some other way to create a raw, raw byte file ? Is there some conversion alternative for an image in Fid format?

  • Fid Image to raw

  • Buffered image to raw

Here I create the file I have indicated the address:

private void  RAWCompression(Fid.Fiv imagen) throws IOException{
    JFileChooser fileChooser = new JFileChooser(System.getProperty("java.library.path"));
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.setSelectedFile(new File("HuellaRaw.raw"));
    fileChooser.showSaveDialog(null);
    File dir = fileChooser.getSelectedFile();
    File file = new File(dir.getAbsolutePath() + "/" + "HuellaRaw.raw");
    System.out.println(file);
    //Create file 
    try {
        file.createNewFile();
    } catch (IOException ex) {
        Logger.getLogger(Capture.class.getName()).log(Level.SEVERE, null, ex);
    }  

I send the image to buferrtd :

BufferedImage imagenBuffered = new BufferedImage(imagen.getWidth(), imagen.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
imagenBuffered.getRaster().setDataElements(0, 0, imagen.getWidth(), imagen.getHeight(), imagen.getData());

I fill the file and convert it to bytes:

//convert BufferedImage to byte 
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(imagenBuffered, "raw", baos);
byte[] bytes = baos.toByteArray();
//FileUtils.writeByteArrayToFile(file, bytes);
try (FileOutputStream os = new FileOutputStream(file)) {
    os.write(bytes);
} catch (Exception e) {
    System.err.print("imagen Error wri " + imagen.getImageData() + "\n");
}

This line does not fill the raw file:
(If I change raw to png then it does fill the file.)

ImageIO.write(imagenBuffered, "raw", baos);

This line of code with png is created correctly:

ImageIO.write(imagenBuffered, "png", baos);

You cannot use ImageIO to write raw. You could get the "raw bytes" of your image and write them to a file though.

If you already have your buffered image, you could try the inverse of this answer. https://stackoverflow.com/a/54578326/2067492

int[] rgb = imagenBuffered.getRGB(0, 0, width, height, null, width);
ByteBuffer bb = ByteBuffer.allocate( rgb.length*4 );
bb.asIntBuffer().put(rgb);

bytes = bb.array();

This would write all of the pixels to a byte[], no headers or information about the data, just 4 byte rgba as returned by getRGB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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