[英]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.问题是
ImageIO.write
无法识别原始格式,因此文件不会填充它。 I tried the same code with png
and it created it correctly.我用
png
尝试了相同的代码,它正确地创建了它。 Is there some alternative to ImageIO.write
or some other way to create a raw, raw byte file ?是否有
ImageIO.write
的替代方法或其他方法来创建原始的原始字节文件? Is there some conversion alternative for an image in Fid format? Fid 格式的图像是否有一些转换选项?
Fid Image to raw Fid 图像到原始
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
:我将图像发送到
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.) (如果我将
raw
更改为png
那么它会填充文件。)
ImageIO.write(imagenBuffered, "raw", baos);
This line of code with png
is created correctly:这行带有
png
的代码已正确创建:
ImageIO.write(imagenBuffered, "png", baos);
You cannot use ImageIO to write raw.您不能使用 ImageIO 来编写原始文件。 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
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.这会将所有像素写入 byte[],没有标头或有关数据的信息,只有 getRGB 返回的 4 字节 rgba。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.