What happened to my java code to rotate a jpeg photo?

I just want the user of my website to change the orientation of the submitted photo from horizontal to vertical. Here is my code:

public static final void rotatePhoto(String jpgFilename){ BufferedImage originalImage = null, newImage=null; try{ File file = new File(jpgFilename); originalImage = ImageIO.read(file); System.out.println("Photo.rotatePhoto(" +jpgFilename +") originalImage.getWidth(null)=" +originalImage.getWidth(null) +" originalImage.getHeight(null)=" +originalImage.getHeight(null) ); java.awt.image.AffineTransformOp opRotated = new java.awt.image.AffineTransformOp( java.awt.geom.AffineTransform.getQuadrantRotateInstance(1), null ); newImage = opRotated.createCompatibleDestImage(originalImage, originalImage.getColorModel()); opRotated.filter(originalImage, newImage); }catch (IOException e){ } /// Write result to file:::::::::::::::::::::::::::::::::::::::::::::::::::: try{ File outputfile = new File(testFilename); ImageIO.write(newImage, "jpg", outputfile); }catch(IOException ioE){ } } 

The problem is that I get this error, although System.out.println shows the width and height of 640x480

 java.awt.image.RasterFormatException: Transformed width (0) is less than or equal to 0. java.awt.image.AffineTransformOp.createCompatibleDestImage(AffineTransformOp.java:447) base.Photo.rotatePhoto(Photo.java:135) base.ProcessContent.handleInput(ProcessContent.java:245) servlets.ProcessServlet.doPost(ProcessServlet.java:74) servlets.ProcessServlet.doGet(ProcessServlet.java:33) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 

Any ideas or workarounds?

+4
source share
2 answers

Try creating a new AffineTransform from scratch and using it in your AffineTransformOp constructor:

 AffineTransform tx = new AffineTransform(); tx.rotate(Math.PI / 2, originalImage.getWidth() / 2, originalImage.getHeight() / 2); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); newImage = op.filter(originalImage, newImage); 

You must also ensure that newImage contains the data returned by the filter() method.

Oddly enough, this will only work if you set the formatName in ImageIO.write() to "png". I tried using jpg and the result was black.

+4
source

Using AffineTransform.getQuadrantRotateInstance(1);

Your AffineTransform rotates a positive number of quadrants along the axis. This will ruin the conversion operation, since it depends on x and y when a compatible image is created.

  int w = rx + r.width; int h = ry + r.height; if (w <= 0) { throw new RasterFormatException("Transformed width ("+w+ ") is less than or equal to 0."); } 

I would recommend doing it yourself:

 public final void rotatePhoto(String jpgFilename) throws IOException { File file = new File(jpgFilename); BufferedImage originalImage = ImageIO.read(file); // You could use Math.PI / 2, depends on your input. AffineTransform affineTransform = new AffineTransform(); affineTransform.rotate(Math.toRadians(90), originalImage.getWidth() / 2, originalImage.getHeight() / 2); // Now lets make that transform an operation, and we use it. AffineTransformOp opRotated = new AffineTransformOp(affineTransform, AffineTransformOp.TYPE_BILINEAR); BufferedImage newImage = opRotated.filter(originalImage, null); // Save the image. File outputfile = new File("rotated.jpg"); ImageIO.write(newImage, "jpg", outputfile); } 

UPDATE: Btw, the answer was previously given. How to write a servlet that rotates images?

+4
source

All Articles