Java旋转图像丢失质量

时间:2011-03-25 17:41:53

标签: java image-processing rotation

我有旋转图像的功能。虽然我正在失去质量。有没有任何改变可以改善它?

 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;
         }

4 个答案:

答案 0 :(得分:2)

AffineTransform过滤参数提到的其他几个参数很重要,但它也取决于图像的编码。如果是JPEG,lossless rotation isn't universally possible

答案 1 :(得分:1)

AffineTransformOp.TYPE_NEAREST_NEIGHBOR总会让一切看起来都像blergh。尝试使用AffineTransformOp.TYPE_BILINEARAffineTransformOp.TYPE_BICUBIC

答案 2 :(得分:1)

Try using bicubic or bilinear。链接显示了每个的示例。

AffineTransformOp op = 
    new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

答案 3 :(得分:1)

尝试使用其中之一 TYPE_BICUBIC

TYPE_BILINEAR