Have a requirement for any given image need to generate 32 bit depth bmp image in Java.
I googled and looked at Java Advance Image processing and Java2D, but couldnt find exact solution. Appreciate if any responses for the same.
Regards,
Rakesh.
May be you should use ImageIO.write(..., "bmp", ...)? I say about ImageIO because you made an accent on BMP. So, you need a BMP bitmap data with BMP header.
Create BufferedImage (http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html) with required parameter and serialize it to BMP.
...
Robot robot = new Robot();
Rectangle screenRectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage image = robot.createScreenCapture(screenRectangle);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "bmp", out);
...
Related
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
My goal here is to read in a CMYK Tiff image, mess with the pixels and then write out a new CMYK Tiff image. I am having a tough time finding any kind of example for this and any help would be greatly appreciated. Originally, I was doing this with RGB JPEG images without a problem using code like this:
// read input image
BufferedImage in = ImageIO.read(inputJPGAsFile);
// made output image that is same size as input image
FinalImage = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
// mess with pixels in output image
// write as JPG
ImageIO.write(FinalImage, "JPEG", outputJPGAsFile);
However, now I am trying to figure out how to change this to work with CMYK Tiffs. It doesn't seem like there is a type for BufferedImage that would be anything like "TYPE_INT_CMYK". So then I was trying to use the ColorModel of the input image, but this crashes with the following error Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Unknown color space. Any help on this would be greatly appreciated.
// read input image
BufferedImage in = ImageIO.read(inputTiffAsFile);
// get the CMYK color space of input image
ColorModel colorModel = in.getColorModel();
BufferedImage FinalImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(100, 100), colorModel.isAlphaPremultiplied(), null);
// Copy in to FinalImage with some pixel manipulation
// write final image
ImageIO.write(FinalImage, "TIFF", outputTiffAsFile);
Also, I put java 9 because that is what I have been using, but I can upgrade to a newer version if that makes things easier. Thanks!
Im using this code in java:
Image img = ImageIO.read(new File("imagepath/file.png").getScaledInstance(300, 300, BufferedImage.SCALE_SMOOTH);
BufferedImage buffered = new BufferedImage(300, 300, BufferedImage.SCALE_FAST);
buffered.getGraphics().drawImage(img, 0, 0 , null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(buffered, "png", os);
InputStream in = new ByteArrayInputStream(os.toByteArray());
return in;
This successfully scales down and shows a thumbnail in the browser using my laptop. However when I'm launch it on my mini server (Raspberry Pi) it is horrible slow. More accurate is about 4 times longer than loading the actual full-res image.
Can anybody tell me how this is even possible? 300x300 < 1280x720! Should be less work and less bandwidth!
Cheers!
getScaledInstance is known to be slow, see for example this article for a detailed explanation.
Note that your
BufferedImage buffered = new BufferedImage(300, 300, BufferedImage.SCALE_FAST);
line is wrong, here for the third argument you should specify the image type ( TYPE_INT_RGB, TYPE_INT_ARGB, TYPE_INT_ARGB_PRE etc) and not SCALE_FAST (which is not even a field in BufferedImage)
Also see this: How to scale a BufferedImage
For quality downscaling see this: Quality of Image after resize very low -- Java
I'm trying to convert an RGB image to a CMYK image using Java.
I've looked at this code:
BufferedImage rgbImage = ImageIO.read(new File("d:\\Temp\\IMG_1748x2480.jpg"));
BufferedImage cmykImage = null;
ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(
TestConvertRGBToCMYK.class.getClassLoader().getResourceAsStream(
"CMYK_Profiles/USWebCoatedSWOP.icc")));
ColorConvertOp op = new ColorConvertOp(rgbImage.getColorModel().getColorSpace(), cpace, null);
cmykImage = op.filter(rgbImage, null);
ImageIO.write(cmykImage, "JPEG", new File("d:\\Temp\\CMYK_Sample_RGB_OUTPUT2.jpg"));
But the problem is that the result image appears black.
After reading the Google point of view (posts) I've read that this is normal, because the views aren't compatible with the CMYK images, but PDF views are. But the problem is if I put the image in the PDF, the image continues to appear black.
Does any one have any idea for what is going one? Or have any other converter that does a better conversion?
Just an update of my problem.
Use ImageMagick software and i get all my problems resolved.
Cheers,
Bigster
I need to read image in java.
Then I should to convert it to 565RGB
In addition it would be good to resize this image to 320 x 240.
How should I do it? Help me please.
I know such information:
1)It is possible to read image by its URL.
ImageIcon imgThisImg = new ImageIcon(imageURL);
2) It is possible to create image instances that supports 565RGB.
BufferedImage bufImg = new BufferedImage(320, 240, BufferedImage.TYPE_USHORT_565_RGB);
3)BufferedImage inherits ImageIcon , so it is possible to perform such operation
Image imgPicture ...
BufferedImage bufImg = (BufferedImage) imgPicture;
But I haven't any idea, will bufImg in this case have BufferedImage.TYPE_USHORT_565_RGB format?
How to stretch, to squeeze or to cut this picture to get size 320 x 240?
The most convenient method to read image from any source (File,Stream,URL) is
BufferedImage bufImg = ImageIO.read( imageURL );
Then to answer your question you should check this post
How to scale a BufferedImage.