antialiasing in java image scaling library - java

I am currently using http://code.google.com/p/java-image-scaling/ this library to generate scaled images for my web app.But when I scale down the image to about 100x100 size there are some leftover artifacts visible in some images. Is this an issue with antialiasing? And how do I use antialiasing with this library.The api documentation doesn't say any thing about it.
Here is the code
File f = new File("C:\\Users\\ad min\\Pictures\\30-whisky-3d-wallpaper-1152x864.jpg");
BufferedImage src = ImageIO.read(f);
//ResampleOp resampleOp = new ResampleOp(76, 76);
ResampleOp resampleOp = new ResampleOp(200,200);
resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.VerySharp);
BufferedImage rescaled = resampleOp.filter(src, null);
ImageIO.write(rescaled, "JPG", new File(
"C:\\Users\\ad min\\Pictures\\scaleddown.jpg"));
what am I doing wrong?

I finally didn't need antialiasing I simply used this code given in the foloowing link and it worked :) whewww
http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-
compression-quality-when-saving-images-in-java
Iterator<ImageWriter> iter = ImageIO
.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter) iter.next();
// instantiate an ImageWriteParam object with default compression
// options
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(1); // an integer between 0 and 1
// 1 specifies minimum compression and maximum quality
File file = new File("C:\\Users\\ad min\\Pictures\\scaleddown.jpg");
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(rescaled, null, null);
writer.write(null, image, iwp);
writer.dispose();

Related

Changing image quality

I'm trying to create remote control application in Java. I'm using robot to capture my screen image, and then I need to send it to the server. However, because the image size may be too big for the sending to be quick as possible, I'm changing the image quality in the code.
The problem is with the code I have, after changing the image it automatically save it as file in my computer but I don't want it to. I want it to the change it without saving it to be able to send it to my server
The code:
Robot robot = null;
Rectangle rectangle = null;
GraphicsEnvironment gEnv=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gDev=gEnv.getDefaultScreenDevice();
//Get screen dimensions
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
rectangle = new Rectangle(dim);
System.out.println(rectangle);
robot = new Robot(gDev);
BufferedImage image = robot.createScreenCapture(rectangle);
// FileInputStream inputStream = new FileInputStream(MyFile);
// BufferedImage originalImage = ImageIO.read(inputStream);
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
float quality = 0.25f; // reduce quality by 50%
iwp.setCompressionQuality(quality);
File file = new File("Tester6.png");
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image1 = new IIOImage(image, null, null);
writer.write(null, image1, iwp);
writer.dispose();
Instead of creating a file, do:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);
You can then use baos.toByteArray() to get the bytes after you have called writer.write().

Java merging multiple JPEG files into single multi-page TIFF file [duplicate]

I am using JAI to load in multipage TIFF images
File file = workArea[0];
SeekableStream s = new FileSeekableStream(file);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
//first page
RenderedImage op1 =
new NullOpImage(dec.decodeAsRenderedImage(0),
null,
OpImage.OP_IO_BOUND,
null);
BufferedImage pg1 = new BufferedImage(op1.getWidth(), op1.getHeight(),
BufferedImage.TYPE_INT_RGB);
pg1.getGraphics().drawImage((Image) op1, 0, 0, null);
However, in the last line I get a runtime error of:
Exception in thread "main" java.lang.ClassCastException:
javax.media.jai.MullOpImage cannot be cast to java.awt.Image
I clear the RenderedImage after attempting to set the BufferedImage so I don't exactly "need" the RenderedImage if there is another method of doing this.
I attempted:
pg1.setData(op1.getData());
and that gives an ArrayIndexOutOfBoundsException. I'm not sure why exactly as pg1's width and height are set by op1's, but there is probably a very valid reason.
I found a solution at http://www.jguru.com/faq/view.jsp?EID=114602
The first one didn't work, however, the convertRenderedImage function did work.
public BufferedImage convertRenderedImage(RenderedImage img) {
if (img instanceof BufferedImage) {
return (BufferedImage)img;
}
ColorModel cm = img.getColorModel();
int width = img.getWidth();
int height = img.getHeight();
WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
Hashtable properties = new Hashtable();
String[] keys = img.getPropertyNames();
if (keys!=null) {
for (int i = 0; i < keys.length; i++) {
properties.put(keys[i], img.getProperty(keys[i]));
}
}
BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
img.copyData(raster);
return result;
}
Use op1.getAsBufferedImage() to create pg1.
If you are stuck with a RenderedImage, you can use
PlanarImage.wrapRenderedImage(renderedImage).getAsBufferedImage()
see here for documentation
JAI apparently has a "converter" class in there:
ImageDecoder dec = ImageCodec.createImageDecoder("PNM", new File(input), null);
return new RenderedImageAdapter(dec.decodeAsRenderedImage()).getAsBufferedImage()
ref: http://www.programcreek.com/java-api-examples/index.php?api=com.sun.media.jai.codec.ImageDecoder
Try this :
RenderedImage im = dec.decodeAsRenderedImage();
BufferedImage bi = PlanarImage.wrapRenderedImage(im).getAsBufferedImage();
The easiest way to load TIFF is using Twelve Monkey with provide plugin to support TIFF format into Standard Java ImageIO.
Just add below Maven dependency,
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.5</version>
</dependency>
Now, you will be able to load TIFF file directly Using ImageIO.
ImageReader imageReader1 = SPI.createReaderInstance();
ImageInputStream iis1 = ImageIO.createImageInputStream(new File("1.tif"));
imageReader1.setInput(iis1);
BufferedImage = imageReader1.read(0);
It is very easy and reliable because it uses standard Java ImageIO API for all processing.
Only the Plugin from Twelve Monkey provide the SPI plugin to provide support of TIFF.
Adding here one example program which is running is Java 8 and it reads TIFF files and create a single multi-page TIFF file:
BufferedImage b1 = null;
BufferedImage b2 = null;
TIFFImageReaderSpi SPI = new TIFFImageReaderSpi();
ImageReader imageReader1 = SPI.createReaderInstance();
ImageInputStream iis1 = ImageIO.createImageInputStream(new File("1.tif"));
imageReader1.setInput(iis1);
b1 = imageReader1.read(0);
ImageReader imageReader2 = SPI.createReaderInstance();
ImageInputStream iis2 = ImageIO.createImageInputStream(new File("2.tif"));
imageReader2.setInput(iis2);
b2 = imageReader2.read(0);
ImageWriter writer = ImageIO.getImageWritersByFormatName("TIFF").next();
writer.setOutput(ImageIO.createImageOutputStream(new File("3.tif")));
ImageWriteParam writeParam = writer.getDefaultWriteParam();
//writeParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
//writeParam.setCompressionType("Deflate");
writer.prepareWriteSequence(null);
IIOImage i1 = new IIOImage(b1, null, null);
IIOImage i2 = new IIOImage(b2, null, null);
writer.writeToSequence(i1, writeParam);
writer.writeToSequence(i2, writeParam);
writer.endWriteSequence();
writer.dispose();
It is working with Java 8, if someone wants to add compression just comment the line and add appropriate compression name.

How can I convert png/gif/jpg in grails to jpg maintaining a high quality in the image?

I use the following method for converting images to jpg. The problem with my solution is that it will reduce the quality to much.
What is a good way to maintain quality of the image, while reducing the file size?
def convertToJpg(currentImage) {
try {
InputStream inStreamCrop = new ByteArrayInputStream(currentImage)
BufferedImage bufferedImage = ImageIO.read(inStreamCrop)
// create a blank, RGB, same width and height, and a white background
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),
bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
ByteArrayOutputStream baos=new ByteArrayOutputStream()
// write to jpeg file
ImageIO.write(newBufferedImage, "jpg", baos);
baos.flush()
def image = baos.toByteArray()
baos.close()
return image
} catch (IOException e) {
e.printStackTrace();
}
}
You can control the quality of the JPEG by getting an ImageWriter object and setting it through the parameters. For example:
import javax.imageio.stream.*
import javax.imageio.*
BufferedImage bufferedImage = ImageIO.read(new File("test.png"));
float quality = 0.9;
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(quality);
writer.setOutput(new FileImageOutputStream(new File("test.jpg")));
writer.write(null, new IIOImage(bufferedImage, null, null), param);
The quality parameter can range between 0 and 1, with 1 having the least compression. Try different values; I've found 0.9 is a good choice for low compression (but large file sizes).
Note that JPEG images are not supported with OpenJDK: ImageIO not able to write a JPEG file

Reduce JPEG image size without scale its width/height by Java

I want to reduce one jpeg image size(3M reduce to 1M) by Java, without scale(no change for image height and width). IN this site, I could not find a solution. Below is what I have tried:
1 Using ImageIO:
BufferedImage image = ImageIO.read(inputFile);
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if(iter.hasNext()){
writer = (ImageWriter) iter.next();
}
ImageOutputStream ios = ImageIO.createImageOutputStream(outputFile);
writer.setOutput(ios);
ImageWriteParam iwParam = writer.getDefaultWriteParam();
iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwParam.setCompressionQuality(compressionQuality);
writer.write(null, new IIOImage(image, null, null ), iwParam);
For solution 1, I set compressionQuality for jpg but I can not obtain origina image compressQuality and the newImage I get sometimes is bigger than originals.
The compression quality used is not stored with the JPEG image.
If you need to get below a certain threshold you must try several times while lowering the compression quality each time until you reach your limit. Be aware that very low settings give bad images.
I am unfamiliar with the MODE_EXPLICIT flag. It might also be a tunable parameter.
I found the following code example for reducing the quality.
The important part is just to set iwp.setCompressionQuality. Hopes this helps.
BufferedImage bi = null;
bi = ImageIO.read(new File("image.jpg"));
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// reduced quality.
iwp.setCompressionQuality(0.1f);
File file = new File("c:/image_low.jpg");
FileImageOutputStream output = null;
output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(bi, null, null);
writer.write(null, image, iwp);
writer.dispose();

image upload resolution problems

when i am uploading an image with image io it is converting the resolutions of the image
what i mean to say is i am uploading a image with 300 dpi it get converts into 96 dpi why?
i am not using any resizing
Guess, you don't copy image metadata, here goes simplistic example how this can be done:
ImageInputStream iis =ImageIO.createImageInputStream(new File("test.jpg"));
ImageReader reader = (ImageReader) ImageIO.getImageReaders(iis).next();
reader.setInput(iis, true);
IIOMetadata meta = reader.getImageMetadata(0);
BufferedImage image = reader.read(0);
/*
do image manipulations here
*/
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("out.jpg"));
ImageWriter writer = ImageIO.getImageWriter(reader);
writer.setOutput(ios);
writer.write(meta, new IIOImage(image, null, null), null);
Since you've not provided any specific details, i've tested it on local files. However, i hope that it provides you a hint.

Categories