I have an int array containing gray scale values from 0-254, i also have the x and y size of the image. It is an easy thing to create an pgm image, but i want to display it in a jsp, so i need somehow to convert it to a jpeg or png image.
If you suggest jai, than please tell me at which classes to look, or how to actually do it in jai.
Thanks a lot, in advance.
Maybe skip the PGM entirely?
int[] myImage = getGreyscaleIntArray();
BufferedImage im = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
WritableRaster raster = im.getRaster();
for(int h=0;h<height;h++)
{
for(int w=0;w<width;w++)
{
raster.setSample(w,h,0, myImage[h * width + w]);
}
}
ByteArrayOutputStream myJpg = new ByteArrayOutputStream();
javax.imageio.ImageIO.write(im, "jpg", myJpg);
uses the JAI ImageIO api, specifically the ImageIO utility class
WriteableRaster sample from the Java Image Processing cookbook
ImageMagick works well for converting images and Jmagick provides an interface to call directly from java programs.
Related
I have two buffered images and I want to check whether there is a part in Image 1 that is exactly the same as Image 2.
BufferedImage image1 = ......;
BufferedImage image2 = ......;
if(image1.contains(image2)) //I'm looking for method like contains
{
//do some work.
}
Thanks in advance.
JavaCV is a good choice for image manipuation in Java. JavaCV is an OpenCV wrapper that contains tons of methods for image processing.
I am attempting to convert a jpeg image in rgb to CMYK colorspace. The only problem is my final output is always a black image. But interesting enough the preview application in MAC shows the image correctly. There does not seem to be an example of a successful rgb to cmyk conversion anywhere I've looked so far. Below is the code i'm using to attempt the conversion. This code works fine If i'm performing the conversion to rgb using RGB ICC Profile. Any guidance is greatly appreciated.
import javax.imageio.ImageIO;
public class TestClass {
public static void main(String[] args) throws Exception {
BufferedImage cmykImage = ImageIO.read(new File(
"CMYK_Sample.jpg"));
BufferedImage rgbImage = null;
ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(TestClass.class.getClassLoader().getResourceAsStream("icc/USWebCoatedSWOP.icc")));
ColorConvertOp op = new ColorConvertOp(cpace, null);
rgbImage = op.filter(cmykImage, null);
ImageIO.write(rgbImage, "JPEG", new File("CMYK_Sample_RGB_OUTPUT2.jpg"));
}
}
CMYK is for printing. So, there are few possibilities to show it, except of pdf and postscript files. JPEG can show almost only RGB. So, in your last line ImageIO.write you are trying to read cmyk as RGB. Here is the problem.
CMYK in JPEG:"Adobe Photoshop and some other prepress-oriented applications will produce
four-channel CMYK JPEG files when asked to save a JPEG from CMYK image mode.
Hardly anything that's not prepress-savvy will cope with CMYK JPEGs (or any
other CMYK format for that matter). When making JPEGs for Web use, be sure
to save from RGB or grayscale mode." (http://www.faqs.org/faqs/jpeg-faq/part1/)
As for showing CMYK files in java, use java-2d (http://download.oracle.com/javase/1.3/docs/guide/2d/spec/j2d-color.fm2.html)
I'm trying to reduce the filesize of a few PDF's in Java. I've found a few techniques to do this. But resizing images seems most effective.
I followed this piece of code which uses iText.
It works great on a few PDF's (reduction from 4.5MB to 800KB!) but some stayed unaffected.
I altered the code a bit as follows, so it would accept more Stream objects (and hopefully compress more):
PdfReader reader = new PdfReader(original.getPath());
int size = reader.getXrefSize();
for(int i=0;i<size;i++){
PdfObject object = reader.getPdfObject(i);
if (object == null || !object.isStream())
continue;
PRStream stream = (PRStream)object;
PdfImageObject image = new PdfImageObject(stream);
try{
BufferedImage bi = image.getBufferedImage();
//more stuff here
And it did! Some PDF's actually got compressed a lot more. However, I got a strange exception at some that didn't change at all:
javax.imageio.IIOException: Incompatible color conversion
at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkColorConversion(JPEGImageReader.java:927)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1028)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:984)
at javax.imageio.ImageIO.read(ImageIO.java:1438)
at javax.imageio.ImageIO.read(ImageIO.java:1342)
at com.itextpdf.text.pdf.parser.PdfImageObject.getBufferedImage(PdfImageObject.java:302)
at com.example.compression.App.main(App.java:56)
(App.java:56 being the line that says image.getBufferedImage();)
Some research discovered, the Stream it was trying to open was a CMYK JPG.
I'm wondering why these wouldn't open, and how I can resize these images anyway.
Thanks
BufferedImage doesn't deal with CMYK nativally (and I'm not sure if Java does in other native components, anyway).
In the fields listed in Java 6 docs, there's no cmyk color model....
You can take a look at this site, someone tells you how to deal with BufferedImage and CMYK colorspace.
Good luck!
I have a servlet based application that is serving images from files stored locally. I have added logic that will allow the application to load the image file to a BufferedImage and then resize the image, add watermark text over the top of the image, or both.
I would like to set the content length before writing out the image. Apart from writing the image to a temporary file or byte array, is there a way to find the size of the BufferedImage?
All files are being written as jpg if that helps in calculating the size.
BufferedImage img = = new BufferedImage(500, 300, BufferedImage.TYPE_INT_RGB);
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
ImageIO.write(img, "png", tmp);
tmp.close();
Integer contentLength = tmp.size();
response.setContentType("image/png");
response.setHeader("Content-Length",contentLength.toString());
OutputStream out = response.getOutputStream();
out.write(tmp.toByteArray());
out.close();
No, you must write the file in memory or to a temporary file.
The reason is that it's impossible to predict how the JPEG encoding will affect file size.
Also, it's not good enough to "guess" at the file size; the Content-Length header has to be spot-on.
Well, the BufferedImage doesn't know that it's being written as a JPEG - as far as it's concerned, it could be PNG or GIF or TGA or TIFF or BMP... and all of those have different file sizes. So I don't believe there's any way for the BufferedImage to give you a file size directly. You'll just have to write it out and count the bytes.
You can calculate the size of a BufferedImage in memory very easily. This is because it is a wrapper for a WritableRaster that uses a DataBuffer for it's backing. If you want to calculate it's size in memory you can get a copy of the image's raster using getData() and then measuring the size of the data buffer in the raster.
DataBuffer dataBuffer = bufImg.getData().getDataBuffer();
// Each bank element in the data buffer is a 32-bit integer
long sizeBytes = ((long) dataBuffer.getSize()) * 4l;
long sizeMB = sizeBytes / (1024l * 1024l);`
Unless it is a very small image file, prefer to use chunked encoding over specifying a content length.
It was noted in one or two recent stackoverflow podcasts that HTTP proxies often report that they only support HTTP/1.0, which may be an issue.
Before you load the image file as a BufferedImage make a reference to the image file via the File object.
File imgObj = new File("your Image file path");
int imgLength = (int) imgObj.length();
imgLength would be your approximate image size though it my vary after resizing and then any operations you perform on it.
So I have this GIF file on my desktop (it's a 52 card deck of poker cards). I have been working on a program that cuts it up into little acm.graphics.GImages of each card. Now, however, I want to write those GImages or pixel arrays to a file so that I can use them later. I thought it would be as straight forward as writing .txt files, but a couple of Google searches later I am more confused than before.
So how do I go about making .gif files out of pixel arrays or GImages (I've got loads of both)?
Something along these lines should do the trick (modify the image type, dimensions and pixel array as appropriate):
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = image.getRaster();
for ( i=0; i<width; i++ ) {
for ( j=0; j<height; j++ ) {
int[] colorArray = getColorForPixel(pixels[i][j]);
raster.setPixel(i, j, colorArray);
}
}
ImageIO.write(image, "gif", new File("CardImage"));
'getColorForPixel' will need to return an array representing the color for this pixel. In this case, using RGB, the colorArray will have three integers [red][green][blue].
Relevant javadoc: WritableRaster, BufferedImage and ImageIO.
I had to create GIF's out of Java Images for a university project, and I found this.
I would recommend Acme's Open-Source GifEncoder Class. Nice and easy to use, I still remember it over 2 years later.
Here's the link: http://www.acme.com/java/software/Acme.JPM.Encoders.GifEncoder.html
And here's the G-Link: http://www.google.com/search?hl=en&q=acme+java+gif&btnG=Search
It doesn't really answer your question directly, but wouldn't it be easier to use ImageMagick? It has Java bindings.