我旋转 jpeg 照片的 java 代码有什么问题?

发布于 2024-11-15 23:20:04 字数 1706 浏览 1 评论 0原文

我只是想让我的网站的用户将提交的照片的方向从水平更改为垂直。这是我的代码:

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){
      }
   }

问题是即使 System.out.println 显示宽度和高度为 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)

有任何想法或解决方法吗?

I simply want to enable the user of my web site to change the orientation of a submitted photo from horizontal to vertical. Here's 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){
      }
   }

Problem is I get this error even though the System.out.println shows the width and height to be 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦归所梦 2024-11-22 23:20:04

尝试从头开始创建一个新的 AffineTransform 并在 AffineTransformOp 构造函数中使用它:

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

您还必须确保 newImage 包含 newImage 返回的数据代码>filter()方法。

奇怪的是,只有当您将 ImageIO.write() 中的 formatName 设置为“png”时,这才有效。我尝试使用jpg,结果是一张黑色的图片。

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 also have to make sure newImage contains the data returned by the filter() method.

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

生来就爱笑 2024-11-22 23:20:04

通过使用: AffineTransform.getQuadrantRotateInstance(1);

您的 AffineTransform 正在按轴旋转正数象限。这会扰乱变换操作,因为它依赖于 x 和 y,每当它创建兼容图像时。

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

我建议你自己做:

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

更新:顺便说一句,之前已经在 How do I write a旋转图像的servlet?

By using: AffineTransform.getQuadrantRotateInstance(1);

Your AffineTransform is rotating by a positive number of quadrants by axis. That will mess up the Transform Operation since it depends on the x and y, whenever it creates the compatible image.

    int w = r.x + r.width;
    int h = r.y + 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, it has been answered before on How do I write a servlet which rotates images?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文