-2

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);
Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    I’m voting to close this question because questions must be in English. – President James K. Polk May 31 '22 at 15:48
  • Java doesn't really support RAW by default. https://stackoverflow.com/questions/54546180/how-to-import-and-export-a-raw-image-in-java you have the options of writing your own encoder, using a library, or saving the image in a different format and using a different conversion utility. – matt Jun 02 '22 at 05:03

1 Answers1

0

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.

matt
  • 10,892
  • 3
  • 22
  • 34