Lossless JPEG Rotate (90/180/270 degrees) in Java?

前端 未结 4 1633
悲哀的现实
悲哀的现实 2020-11-30 02:42

Is there a Java library for rotating JPEG files in increments of 90 degrees, without incurring image degradation?

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 03:22

    You don't need an external library for this kind of thing, it's all built into SE. The easiest being the rotate() function of the Graphics2D object.

    For example:

       Image rotatedImage = new BufferedImage(imageToRotate.getHeight(null), imageToRotate.getWidth(null), BufferedImage.TYPE_INT_ARGB);
    
        Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
        g2d.rotate(Math.toRadians(90.0));
        g2d.drawImage(imageToRotate, 0, -rotatedImage.getWidth(null), null);
        g2d.dispose();
    

    no loss!

    Or, if you want to be extra careful, just use BufferedImage.getRGB(x,y), and translate it pixel by pixel on to the new image.

提交回复
热议问题