How can i convert *.raw files to *.bmp in JAVA? - java

BufferedImage image = ImageIO.read( new ByteArrayInputStream( byteArray ) );
ImageIO.write(image, "BMP", new File("filename.bmp"));
When i tried this thing [ImageIO.read( new ByteArrayInputStream( byteArray ))] returns null value so i cant create new bmp file.
But this works to convert jpg to bmp files.I have raw files and i need to convert to image.
Please help me in this.

The support for readers and writers in the javax.imageio is limited, and if you dont know the format of the byte[] your are trying to convert, then propably it is not included in the list of valid image readers. Then, what you might need is the Java Advanced Imaging API, which you can find here.
These are the readers a
Readers: Writers:
bmp bmp
jpg jpg
jpeg jpeg
wbmp Wbmp
png png
gif gif
This few lines shall help you to get this to work using JAI API
// read byte[]
SeekableStream stream = new ByteArraySeekableStream(image);
// render stream of bytes to valid image format
RenderedImage renderedImage = JAI.create("stream", stream);
// persist image to file
JAI.create("filestore", renderedImage, filename, targetFormat);
// dont't forget to close the stream!
stream.close();

Take a look here: Java Advanced Imaging I/O Tools. The Java Image I/O Reader seems to be able to read "raw"-Files. I think that you can write them with the Java Image I/O Writer as BMP.
regards
Macs

Related

Java ImageIO read image byte[] into pre-allocated BufferedImage

I'm working with ImageIO and JAI and want to read a byte array into a BufferedImage. The byte[] contains data for a JP2000 encoded image, and it's fairly large, around 100MB. I'm currently doing something like:
byte[] imageDataBytes = ...
InputStream imageStream = new ByteArrayInputStream(imageDataBytes);
BufferedImage imageData = ImageIO.read(imageStream);
It seems that ImageIO is creating a new BufferedImage each time read() is called.
Question:
Is there a way to tell ImageIO to read and decode the image byte data into a pre-allocated mutable BufferedImage?
I did some searching through the Javadocs and found that the BufferedImage stores its data in a Raster object, which stores its data in a DataBuffer object. So I'm aware any solution that exists will technically not be writing to the BufferedImage, but instead will be directly writing to the DataBuffer.
It may help to know that all images are the same size: roughly 10,000 x 10,000, so there shouldn't be any problems with the read image not aligning with the buffered image. Ultimately, I would like to have an object pool of buffered images, or rasters, or data buffers, and borrow from the pool every time I read using ImageIO. Something like this pseudocode:
InputStream imageStream = new ByteArrayInputStream(imageDataBytes);
WritableRaster raster = ObjectPool.getAvailableRaster();
ImageIO.readToRaster(imageStream, raster);
BufferedImage imageData = new BufferedImage(raster);
I'm sure there's a simple solution out there. Any help would be appreciated!
Yes, you can set the destination image of an ImageReadParam object. However, there is a caveat: the BufferedImage must have a ColorModel and SampleModel that match the image being loaded.
I’m not sure about JPEG2000 images, but regular JPEGs are usually RGB images, so an image of TYPE_INT_RGB should suffice:
BufferedImage image = new BufferedImage(10000, 10000,
BufferedImage.TYPE_INT_RGB);
while (bytesAvailable) {
byte[] imageDataBytes = getImageBytes();
try (InputStream in = new ByteArrayInputStream(imageDataBytes);
ImageInputStream stream = ImageIO.createImageInputStream(in)) {
ImageReader reader = ImageIO.getImageReaders(stream).next();
reader.setInput(stream);
ImageReadParam param = reader.getDefaultReadParam();
param.setDestination(image);
reader.read(0, param);
}
}
For those who find themselves in this situation, the answer by VGR works well. I like to add that specifically for JPEG-2000 images that contain metadata, use
reader.setInput(stream, true, true);
instead of
reader.setInput(stream);
This avoids a NullPointer exception. you can read more about it here:
https://issues.apache.org/jira/browse/PDFBOX-2103

Dealing with Adobe CMYK jpegs and Bufferedimage in java

Im trying to apply the code posted in this post:
How to convert from CMYK to RGB in Java correctly?
The Answer from the guy named Codo works for me so far, but my source is not a file, its an object that gets converted into a BufferedImage with
stream = (PRStream)object;
PdfImageObject image = new PdfImageObject(stream);
//this does not work
BufferedImage bi = image.getBufferedImage();
The guy has a method that returns a BufferedImage from a file like so
public BufferedImage readImage(File file) throws IOException, ImageReadException
but i want to use
BufferedImage bi = readImage(image.getBufferedImage());
instead of
File f = new File("/Users/adlib/Documents/projekte/pdf_compress/envirement/eclipse_luna/WORKSPACE/PDFCompression/src/Bild.jpg");
BufferedImage bi = readImage(f);
cause im ectracting all the images from a pdf file using iText.
I messed around with the code (changed file to BufferedImage and added streams) but a just dont get it to work. The File as Input image works fine, but not really what i need. What do i need to change to get This guys code to work with BufferedImage as input for the readImage() method?
Here is the complete code of this guy
https://stackoverflow.com/a/12132630/4944643
He uses Sanselan / Apache Commons Imaging
I'm not sure how iText extracts images, but chances are good it's using ImageIO. If so, you can just install (or depend on, using Maven) the TwelveMonkeys JPEG ImageIO plugin, and
BufferedImage bi = image.getBufferedImage();
...should just work.
The above mentioned plugin does support CMYK (and Adobe YCCK) JPEGs.
If iText doesn't use ImageIO, the above won't work (ie. when you already have a BufferedImage, it's too late to make the correct conversion). You will instead need to get to the bytes of the underlying PDF (using the getImageAsBytes() method), and use ImageIO (via the TwelveMonkeys JPEG plugin) to decode it:
byte[] imgBytes = image.getImageAsBytes();
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(imgBytes));

What is the difference between using IOUtils and ImageIO for writing an image file

I have a tiff image stored as Base64 encoded String in a file. My aim is to create a tiff file out of it. This is what I am doing:
String base64encodedTiff = IOUtils.toString(new FileInputStream("C:/tiff-attachment.txt"));
byte[] imgBytes = DatatypeConverter.parseBase64Binary(base64encodedTiff);
BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(imgBytes));
ImageIO.write(bufImg, "tiff", new File("c:/new-darksouls-imageIO-tiff.tiff"));
ImageIO.write() is throwing IllegalArgumentException because bufImg is null. I don't understand what am I doing wrong here.
On the contrary if I use IOUtils to write, it works fine:
IOUtils.write(imgBytes, new FileOutputStream("c:/new-darksouls-io-tiff.tiff"));
Please help me understand
Why ImageIO is throwing exception
What is the right API and way for what I am trying to achieve.
ImageIO would be useful if, for example, you wanted to convert a PNG to a JPEG. Since you don't need to manipulate the image or convert to another format, don't bother with ImageIO. Just use IOUtils.write() to save the TIFF data verbatim.
ImageIO.read() is returning a null image because it can't read the TIFF file, probably because TIFF isn't one of the standard ImageIO plugin formats. The standard supported image formats are listed here:
http://docs.oracle.com/javase/6/docs/api/javax/imageio/package-summary.html
An additional note -- the code you posted buffers the entire image in memory. If you're concerned about using memory efficiently, consider using some kind of Base64 decoding input stream to perform the decoding on the fly. That might look like this:
try (FileOutputStream out = new FileOutputStream("c:/new-darksouls-io-tiff.tiff");
FileInputStream in = new FileInputStream("C:/tiff-attachment.txt");
Base64InputStream decodedIn = new Base64InputStream(in)) {
IOUtils.copy(decodedIn, out);
}

Apache Batik - Exception: "content not allowed in prolog" converting PDF to JPEG

I am getting the an exception when I try to convert some PDF to JPEG, with the message "content not allowed in prolog". I am performing a two-step opertaion converting SVG to PDF and then PDF to image.
I am facing this issue when I try to do the direct process which Batik is meant for.
Here is my code.
File pdfFile= new File("path to pdffile");
InputStream in = new java.io.FileInputStream(pdfFile);
JPEGTranscoder transcoder = new JPEGTranscoder();
transcoder.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME,
"org.apache.crimson.parser.XMLReaderImpl");
transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(1.0));
TranscoderInput input = new TranscoderInput(in);
OutputStream ostream = new FileOutputStream("path to jp file here!");
TranscoderOutput output = new TranscoderOutput(ostream);
transcoder.transcode(input, output);
ostream.close();
Batik transcoders expect to be given SVG data as input, not pdf documents.
I can't find anything that suggests you can convert PDF to JPEG with Batik. Btw, Batik is an XML graphics library, not a general transcoder library. Converting from PDF to JPEG is not what it's for, it's for displaying and/or transcoding SVG input to some image output.
Now, that being said, is there any reason you can't go straight from SVG to JPEG? Or use Batik to do the transcoding from SVG to PDF, then use another library besides Batik to transcode PDF to JPEG? What are your concerns involving this approach?

Uncompress a TIFF file without going through BufferedImage

I am receiving large size CCITT Group 4 compressed TIFF files that need to be written elsewhere as uncompressed TIFF files. I am using the jai_imageio TIFF reader and writer to do that and it works well as long as the product _width * height_ of the image fits in an integer.
Here is the code I am using:
TIFFImageReaderSpi readerSpi= new TIFFImageReaderSpi();
ImageReader imageReader = readerSpi.createReaderInstance();
byte[] data = blobManager.getObjectForIdAndVersion(id, version);
ImageInputStream imageInputStream = ImageIO.createImageInputStream(data);
imageReader.setInput(imageInputStream);
TIFFImageWriterSpi writerSpi = new TIFFImageWriterSpi();
ImageWriter imageWriter = writerSpi.createWriterInstance();
ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
//bufferFile is created in the constructor
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(bufferFile);
imageWriter.setOutput(imageOutputStream);
//Now read the bitmap
BufferedImage bufferedImage = imageReader.read(0);
IIOImage iIOImage = new IIOImage(bufferedImage, null, null);
//and write it
imageWriter.write(null, iIOImage, imageWriteParam);
Unfortunately, the files that I receive are often very large and the BufferedImage cannot be created.
I have been trying to find a way to stream from the ImageReader directly to the ImageWriter but I cannot find out how to do that.
Anybody with a suggestion?
I've had the some issues, and the end result might surprise you :
I ended up calling IrfanView with some command-line options using the Runtime.exec() method. That way, I am not worried about compression or size, it just works and outputs the correct files in the correct folder for me.
If you are on Linux, you can use ImageMagik or something similar.
You can use TIFF tiles to segment a TIFF into smaller portions ("tiles"). If you control the code creating the big images, JAI allows you to retrieve image content tile-by-tile.
Here is an example on how to create tiled image with JAI:
ColorModel cm = source.createColorModel();
// SampleModel with the tilesize
SampleModel sm = cm.createCompatibleSampleModel(tileWidth, tileHeight);
TiledImage image = new TiledImage(0, 0, imageWidth, imageHeight, 0, 0, sm, cm);
TIFFEncodeParam tep = new TIFFEncodeParam();
tep.setTileSize(tileWidth, tileHeight); // Set tile size to avoid OOM
tep.setWriteTiled(true);
JAI.create("filestore", image, filepath, "TIFF", tep);
If you can't control the TIFF production, my knowledge of JAI is too limited to be of much help.
Give your Java VM more memory.
If that doesn't work, look at the source code of the TIFF plugin in the JAI source code. You might be able to write your own processor which just decompresses the data structures using a streaming approach (so you'll never have to keep the whole image in memory at any time).
If that also doesn't work, look at JNA which allows you to call code from a DLL from Java (no C code required; everything is done from pure Java, unlike with Sun's JNI API).

Categories

Resources