Java Rotate Image Lost Quality
I have this function for rotating images.Although i am loosing quality.Any there any change which can improve it ?
public BufferedImage RotateImage(String imagePath,int degrees) throws IOException{
File file = new File(imagePath);
Image image = ImageIO.read(file);
BufferedImage img=bufferImage(image, BufferedImage.TYPE_INT_RGB);
AffineTransform tx = new AffineTransform();
double radians = (Math.PI / 180) * degrees;
double width = img.getWidth()/2;
double height = img.getHeight()/2;
if(degrees != 180){
tx.translate(height,width);
开发者_开发知识库 tx.rotate(radians);
tx.translate(-width,-height);
}else{
tx.rotate(radians,width, height);
}
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
img = op.filter(img, null);
return img;
}
Try using bicubic or bilinear. Link shows an example of each.
AffineTransformOp op =
new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
The AffineTransform filtering parameter several others mentioned is important, but it also depends on the encoding your images come in. If it's JPEG, lossless rotation isn't universally possible.
AffineTransformOp.TYPE_NEAREST_NEIGHBOR
will always make everything look like blergh. Try using AffineTransformOp.TYPE_BILINEAR
or AffineTransformOp.TYPE_BICUBIC
.
Try using either TYPE_BICUBIC
or
TYPE_BILINEAR
精彩评论