I am having a bufferd-image i.e:
BufferedImage buffer = ImageIO.read(new File(file));
now i want to rotate it.. So how i can do it??
Previously i have used the image format i.e:
Image image = ImageIO.read(new File(file));
and could easily rotate a image using:
AffineTransform at = new AffineTransform();
at.rotate(0.5 * angle * Math.PI, width/2, height/2);
But i dont noe how to do it with the bufferd-image?? Can you help me??
Example:
BufferedImage buffer = ImageIO.read(new File(file));
AffineTransform tx = new AffineTransform();
//tx.scale(scalex, scaley);
//tx.shear(shiftx, shifty);
//tx.translate(x, y);
tx.rotate(radians, buffer.getWidth()/2, buffer.getHeight()/2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
buffer = op.filter(buffer, null);
See also:
Rotate an image in java
Rotating BufferedImage instances
AffineTransform truncates image, what do I wrong?
Related
I'm trying to fixing photo direction using EXIF information, the photo rotated correctly but after rotate they becomes very low quality...My guess is parameter passed during write new image is wrong. Any help appreciated.
//code get Exif information
Metadata metadata = ImageMetadataReader.readMetadata(outputFile);
Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
if(directory == null) {
logger.warn("no EXIF info.");
outputFile.delete();
return;
}
JpegDirectory jpegDirectory = metadata.getFirstDirectoryOfType(JpegDirectory.class);
int orientation;
try {
orientation = directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
if(orientation != 1) {
//rotate image
int w = jpegDirectory.getImageWidth();
int h = jpegDirectory.getImageHeight();
ImageInformation imageInformation = new ImageInformation(orientation, w, h);
AffineTransform affineTransform = getExifTransformation(imageInformation);
InputStream pictureStream = new FileInputStream(outputFile);
BufferedImage pictureBuffer = ImageIO.read(pictureStream);
pictureStream.close();
if (pictureBuffer == null) {
logger.warn("The picture buffer parsed is null.");
}
pictureBuffer = transformImage(pictureBuffer, affineTransform);
//code do image transfer
public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception {
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
BufferedImage destinationImage = op.createCompatibleDestImage(image, null );
Graphics2D g = destinationImage.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight());
destinationImage = op.filter(image, destinationImage);
return destinationImage;
}
Thanks all the helps:-)
After change transform function to this, problem solved, not sure why it's the case, gpasch may be right
public static BufferedImage transformImage(BufferedImage image, AffineTransform transform) throws Exception {
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
BufferedImage destinationImage = new BufferedImage(image.getWidth(),image.getHeight(), image.getType());
destinationImage = op.filter(image, destinationImage);
return destinationImage;
}
This may solve your problem. According to AffineTransformOp
"If destCM is null, an appropriate ColorModel is used; this ColorModel may
have an alpha channel even if the source ColorModel is opaque."
Therefore I suggest the following:
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
BufferedImage destinationImage = op.createCompatibleDestImage(image, null );
destinationImage = op.filter(image, null);
return destinationImage;
or even forgo the compatible image:
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
BufferedImage destinationImage = op.filter(image, null);
return destinationImage;
Also I'm not sure Bicubic is all that important but may not be the problem.
Because the compatible image returns an image with alpha i.e. transparent
this
Graphics2D g = destinationImage.createGraphics();
g.setBackground(Color.WHITE);
g.clearRect(0, 0, destinationImage.getWidth(), destinationImage.getHeight());
will put a layer of transparency on the image; the image painted afterward with be fused with the white color.
Keep it simple and you should use the dimensions expected from the operation:
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage destinationImage = op.filter(bImage, op.createCompatibleDestImage(bImage, null));
I have a large PNG image (600x600) and my application makes the image opaque and writes out the file. The problem is that the performance with ImageIO is terrible. Are there any other alternatives? I require the image to be opaque. Below is what I am doing:
BufferedImage buf = ImageIO.read(localUrl);
float[] scales = {1f, 1f, 1f, 1f}; // R, G, B, A
float[] offsets = {0f, 0f, 0f, 1f}; // R, G, B, A
RescaleOp rescaler = new RescaleOp(scales, offsets, null);
BufferedImage opaque = rescaler.filter(buf, null);
File outputfile = new File(localUrl.getPath());
ImageIO.write(opaque, "png", outputfile);
Using a RescaleOp isn't entirely necessary here if you just want to get rid of transparency. A simpler solution would be drawing the image on a background like so:
Color bgColor = Color.WHITE;
BufferedImage foreground = ImageIO.read(localUrl);
int width = foreground.getWidth();
int height = foreground.getHeight();
BufferedImage background = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = background.createGraphics();
g.setColor(bgColor);
g.fillRect(0, 0, width, height);
g.drawImage(foreground, 0, 0, null);
g.dispose();
File outputfile = new File(localUrl.getPath());
ImageIO.write(background, "png", outputfile);
This seems like a simpler method of doing things and would probably require less processing power, but I doubt there would be a huge difference. If you're not satisfied with the speed the image can be read / written from the hard drive, there's little you can do to speed that up.
With PNGJ:
private static void removeAlpha(File file1,File file2) {
PngReaderByte reader = new PngReaderByte(file1);
ImageInfo info = reader.imgInfo;
PngWriter writer = new PngWriter(file2,info);
writer.setFilterPreserve(true);
writer.setCompLevel(6);
writer.copyChunksFrom(reader.getChunksList(), ChunkCopyBehaviour.COPY_ALL_SAFE);
if( info.bitDepth != 8 ||info.channels!=4) throw new RuntimeException("expected 8bits RGBA ");
while(reader.hasMoreRows()) {
ImageLineByte line = reader.readRowByte();
byte [] buf = line.getScanlineByte();
for(int i=0,j=3;i<info.cols;i++,j+=4)
buf[j]=(byte)255;
writer.writeRow(line);
}
reader.end();
writer.end();
}
I'm not sure if this would enhance the perfomance. Bear in mind, also that (contrarily to Parker Hoyes' answer) this simply kills the alpha channel, but it does not blend with some background color (hence the "original" color will appear in the previously-transparent now-opaque regions).
I am currently scaling images using the following code.
Image scaledImage = img.getScaledInstance( width, int height, Image.SCALE_SMOOTH);
BufferedImage imageBuff = new BufferedImage(width, scaledImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics g = imageBuff.createGraphics();
g.drawImage(scaledImage, 0, 0, new Color(0, 0, 0), null);
g.dispose();
ImageIO.write(imageBuff, "jpg", newFile);
Anyone have an idea of a better way of scaling an image and getting better quality results, or even any help on improving my current code to get better quality output.
You can use Affine Transorm
public static BufferedImage getScaledImage(BufferedImage image, int width, int height) throws IOException {
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
double scaleX = (double)width/imageWidth;
double scaleY = (double)height/imageHeight;
AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
return bilinearScaleOp.filter(
image,
new BufferedImage(width, height, image.getType()));
}
Also try this Example .
Also Try java-image-scaling library
You might want to look at this image scaling library. It has algorithms like bicubic and Lanczos and also an unsharp filter.
Try avoiding Image.getScaledInstance().
I'm trying to rotate an image using this code:
File imgPath = new File("c:\\tmp\\7.jpg");
BufferedImage src = ImageIO.read(imgPath);
AffineTransform tx = new AffineTransform();
int width = src.getWidth();
int height = src.getHeight();
tx.rotate(radiant ,width, height);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC);
BufferedImage out = op.filter(src, null);
File outFile = new File("c:\\tmp\\out.jpg");
ImageIO.write(out, "jpg", outFile);
For some reason the background after the rotation is black.
How can make the background white or transparent?
When you are using AffineTransformOp.filter(src, null) for creating new images, the new image uses the same ColorModel as the source image.
Your input image is a jpeg, which means it is not transparent, so the destination image is an RGB image, without the alpha (transparency) level.
When rotated with such a small angle, the image no longer occupies exactly the same bounds, so the background is visible in its edges and because there is no alpha level, it is normal that the background is black.
However, if you save it to a format that supports transparency like gif or png, your image will not display the black background anymore.
ImageIO.write(out, "gif", outFile);
The full code:
try {
File imgPath = new File("d:\\downloads\\about.jpg");
BufferedImage src = ImageIO.read(imgPath);
AffineTransform tx = new AffineTransform();
int width = src.getWidth();
int height = src.getHeight();
tx.rotate(0.02050493823247637, width, height);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC);
BufferedImage out = op.filter(src, null);
File outFile = new File("d:\\downloads\\about0.gif");
ImageIO.write(out, "gif", outFile);
} catch (Exception e) {
e.printStackTrace();
}
Take a look at this for even more information and tricks.
Here is my image after rotation to gif:
I'm using a RenderedImage to display tiffs in a DisplayJAI in my app.
Somebody know how to resize an instance of RenderedImage??
It's been a long time since I've done anything with JAI, but wouldn't the "Scale" or "Affine" operations suffice?
Edit: Here are a couple of links into the Programmer's Guide for "Scale" and "Affine".
public static RenderedImage scale(RenderedImage image, float scaleFactor)
RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
RenderedOp resizeOp = SubsampleAverageDescriptor.create(image,
Double.valueOf(scaleFactor), Double.valueOf(scaleFactor), hints);
BufferedImage bufferedResizedImage = resizeOp.getAsBufferedImage();
return bufferedResizedImage;
}
There is an example code posted here to just that:
http://answers.yahoo.com/question/index?qid=20090827075608AA12kEZ
Relevant code:
BufferedImage img = ImageIO.read(new File("~/your/file/system/example.jpeg"));
BufferedImage thumb = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_RGB);
thumb.createGraphics().drawImage(
img.getScaledInstance(w2, h2, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
File file = new File(fullpath + filename);
ImageIO.write(thumb, "png", file);