【问题标题】:Resizing image in Java在 Java 中调整图像大小
【发布时间】:2011-08-19 05:52:16
【问题描述】:

我有一个 PNG 图像,我想调整它的大小。我怎样才能做到这一点?虽然我已经通过this 看不懂sn-p。

【问题讨论】:

  • 你对那个sn-p到底有什么不明白的?
  • 您想生成原始 png 的调整大小版本,还是只是在 UI 中的某个位置绘制调整大小的版本?

标签: java swing user-interface image


【解决方案1】:

如果您有 java.awt.Image,调整其大小不需要任何额外的库。做吧:

Image newImage = yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);

显然,替换 newWidthnewHeight 为指定图像的尺寸。
注意最后一个参数:它告诉运行时您要用于调整大小的算法

有些算法可以产生非常精确的结果,但是这些算法需要很长时间才能完成。
您可以使用以下任何一种算法:

请参阅Javadoc 了解更多信息。

【讨论】:

【解决方案2】:

我们这样做是为了创建图像的缩略图:

  BufferedImage tThumbImage = new BufferedImage( tThumbWidth, tThumbHeight, BufferedImage.TYPE_INT_RGB );
  Graphics2D tGraphics2D = tThumbImage.createGraphics(); //create a graphics object to paint to
  tGraphics2D.setBackground( Color.WHITE );
  tGraphics2D.setPaint( Color.WHITE );
  tGraphics2D.fillRect( 0, 0, tThumbWidth, tThumbHeight );
  tGraphics2D.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
  tGraphics2D.drawImage( tOriginalImage, 0, 0, tThumbWidth, tThumbHeight, null ); //draw the image scaled

  ImageIO.write( tThumbImage, "JPG", tThumbnailTarget ); //write the image to a file

【讨论】:

    【解决方案3】:

    试试这个:

    ImageIcon icon = new ImageIcon(UrlToPngFile);
    Image scaleImage = icon.getImage().getScaledInstance(28, 28,Image.SCALE_DEFAULT);
    

    【讨论】:

    • IIRC,这种方式 (getSclaedInstance()) 不建议使用,因为它缺乏性能(我想我很久以前在“肮脏的富客户端”中读到过)。
    • 不用ImageIcon,直接ImageIO.read(...)即可。
    • @AndrewThompson 您的链接似乎已过期。新位置是:community.oracle.com/docs/DOC-983611
    • 谢谢@BrianMatthews。不幸的是,我无法编辑原始评论,因此我将在此处对其进行更新:了解更多信息。见The Perils of Image.getScaledInstance()
    【解决方案4】:

    以高质量调整图像大小:

    private static InputStream resizeImage(InputStream uploadedInputStream, String fileName, int width, int height) {
    
            try {
                BufferedImage image = ImageIO.read(uploadedInputStream);
                Image originalImage= image.getScaledInstance(width, height, Image.SCALE_DEFAULT);
    
                int type = ((image.getType() == 0) ? BufferedImage.TYPE_INT_ARGB : image.getType());
                BufferedImage resizedImage = new BufferedImage(width, height, type);
    
                Graphics2D g2d = resizedImage.createGraphics();
                g2d.drawImage(originalImage, 0, 0, width, height, null);
                g2d.dispose();
                g2d.setComposite(AlphaComposite.Src);
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2d.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    
                ImageIO.write(resizedImage, fileName.split("\\.")[1], byteArrayOutputStream);
                return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            } catch (IOException e) {
                // Something is going wrong while resizing image
                return uploadedInputStream;
            }
        }
    

    【讨论】:

    • 为什么要在drawImage()和dispose()之后设置渲染提示?
    【解决方案5】:
                int newHeight = 150;
                int newWidth = 150; 
                holder.iv_arrow.requestLayout();
                holder.iv_arrow.getLayoutParams().height = newHeight;
                holder.iv_arrow.getLayoutParams().width = newWidth;
                holder.iv_arrow.setScaleType(ImageView.ScaleType.FIT_XY);
                holder.iv_arrow.setImageResource(R.drawable.video_menu);
    

    【讨论】:

      【解决方案6】:

      Java 中的简单方法

      public void resize(String inputImagePath,
                  String outputImagePath, int scaledWidth, int scaledHeight)
                  throws IOException {
              // reads input image
              File inputFile = new File(inputImagePath);
              BufferedImage inputImage = ImageIO.read(inputFile);
       
              // creates output image
              BufferedImage outputImage = new BufferedImage(scaledWidth,
                      scaledHeight, inputImage.getType());
       
              // scales the input image to the output image
              Graphics2D g2d = outputImage.createGraphics();
              g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
              g2d.dispose();
       
              // extracts extension of output file
              String formatName = outputImagePath.substring(outputImagePath
                      .lastIndexOf(".") + 1);
       
              // writes to output file
              ImageIO.write(outputImage, formatName, new File(outputImagePath));
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-01
        • 2011-10-26
        • 2013-11-12
        • 1970-01-01
        • 1970-01-01
        • 2015-04-16
        相关资源
        最近更新 更多