Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java n=零; BuffereImage ScaleImage=null; 试一试{ url=getClass().getResource(名称); 图标=新图像图标(url); 如果(图标==null){ System.out.println(“找不到”+url); } BuffereImage bi=新的BuffereImage( icon.getIconWidth(), icon.getIconHeight(), BuffereImage.TYPE_INT_RGB); Graphics g=bi.createGraphics(); //将图标绘制到BuffereImage。 icon.paintIcon(null,g,0,0); g、 处置(); bi=调整图像大小(bi,30,30); scaleImage=bi;//或替换为此行Scalr.resize(bi,30,30); imgicon=新图像图标(缩放图像); }捕获(例外e){ System.out.println(“找不到“+getClass().getName()+”/“+name”); e、 printStackTrace(); } 返回imgicon; } 公共静态缓冲区图像大小图像(缓冲区图像、int areaWidth、int areaHeight){ float scaleX=(float)areaWidth/image.getWidth(); float scaleY=(float)区域高度/image.getHeight(); float scale=Math.min(scaleX,scaleY); int w=Math.round(image.getWidth()*比例); int h=Math.round(image.getHeight()*比例); int type=image.getTransparency()==Transparency.不透明?BufferedImage.type\u int\u RGB:BufferedImage.type\u int\u ARGB; 布尔scaleDown=比例w | |当前h>h){ currentW=数学最大值(w,currentW/2); currentH=数学最大值(h,currentH/2); BuffereImage temp=新的BuffereImage(currentW、currentH、type); Graphics2D g2=临时createGraphics(); g2.setRenderingHint(RenderingHits.KEY\u插值,RenderingHits.VALUE\u插值\u双线性); g2.drawImage(已调整大小,0,0,currentW,currentH,null); g2.dispose(); 调整大小=温度; } 返回调整大小; }否则{ 对象提示=比例>2?RenderingHits.VALUE\u INTERPOLATION\u双三次:RenderingHits.VALUE\u INTERPOLATION\u双线性; BuffereImage resized=新的BuffereImage(w,h,BuffereImage.TYPE_INT_ARGB); Graphics2D g2=已调整大小。createGraphics(); g2.setRenderingHint(RenderingHints.KEY\u插值,hint); g2.drawImage(图像,0,0,w,h,空); g2.dispose(); 返回调整大小; } }_Java_Image_Resize - Fatal编程技术网

Java n=零; BuffereImage ScaleImage=null; 试一试{ url=getClass().getResource(名称); 图标=新图像图标(url); 如果(图标==null){ System.out.println(“找不到”+url); } BuffereImage bi=新的BuffereImage( icon.getIconWidth(), icon.getIconHeight(), BuffereImage.TYPE_INT_RGB); Graphics g=bi.createGraphics(); //将图标绘制到BuffereImage。 icon.paintIcon(null,g,0,0); g、 处置(); bi=调整图像大小(bi,30,30); scaleImage=bi;//或替换为此行Scalr.resize(bi,30,30); imgicon=新图像图标(缩放图像); }捕获(例外e){ System.out.println(“找不到“+getClass().getName()+”/“+name”); e、 printStackTrace(); } 返回imgicon; } 公共静态缓冲区图像大小图像(缓冲区图像、int areaWidth、int areaHeight){ float scaleX=(float)areaWidth/image.getWidth(); float scaleY=(float)区域高度/image.getHeight(); float scale=Math.min(scaleX,scaleY); int w=Math.round(image.getWidth()*比例); int h=Math.round(image.getHeight()*比例); int type=image.getTransparency()==Transparency.不透明?BufferedImage.type\u int\u RGB:BufferedImage.type\u int\u ARGB; 布尔scaleDown=比例w | |当前h>h){ currentW=数学最大值(w,currentW/2); currentH=数学最大值(h,currentH/2); BuffereImage temp=新的BuffereImage(currentW、currentH、type); Graphics2D g2=临时createGraphics(); g2.setRenderingHint(RenderingHits.KEY\u插值,RenderingHits.VALUE\u插值\u双线性); g2.drawImage(已调整大小,0,0,currentW,currentH,null); g2.dispose(); 调整大小=温度; } 返回调整大小; }否则{ 对象提示=比例>2?RenderingHits.VALUE\u INTERPOLATION\u双三次:RenderingHits.VALUE\u INTERPOLATION\u双线性; BuffereImage resized=新的BuffereImage(w,h,BuffereImage.TYPE_INT_ARGB); Graphics2D g2=已调整大小。createGraphics(); g2.setRenderingHint(RenderingHints.KEY\u插值,hint); g2.drawImage(图像,0,0,w,h,空); g2.dispose(); 返回调整大小; } }

Java n=零; BuffereImage ScaleImage=null; 试一试{ url=getClass().getResource(名称); 图标=新图像图标(url); 如果(图标==null){ System.out.println(“找不到”+url); } BuffereImage bi=新的BuffereImage( icon.getIconWidth(), icon.getIconHeight(), BuffereImage.TYPE_INT_RGB); Graphics g=bi.createGraphics(); //将图标绘制到BuffereImage。 icon.paintIcon(null,g,0,0); g、 处置(); bi=调整图像大小(bi,30,30); scaleImage=bi;//或替换为此行Scalr.resize(bi,30,30); imgicon=新图像图标(缩放图像); }捕获(例外e){ System.out.println(“找不到“+getClass().getName()+”/“+name”); e、 printStackTrace(); } 返回imgicon; } 公共静态缓冲区图像大小图像(缓冲区图像、int areaWidth、int areaHeight){ float scaleX=(float)areaWidth/image.getWidth(); float scaleY=(float)区域高度/image.getHeight(); float scale=Math.min(scaleX,scaleY); int w=Math.round(image.getWidth()*比例); int h=Math.round(image.getHeight()*比例); int type=image.getTransparency()==Transparency.不透明?BufferedImage.type\u int\u RGB:BufferedImage.type\u int\u ARGB; 布尔scaleDown=比例w | |当前h>h){ currentW=数学最大值(w,currentW/2); currentH=数学最大值(h,currentH/2); BuffereImage temp=新的BuffereImage(currentW、currentH、type); Graphics2D g2=临时createGraphics(); g2.setRenderingHint(RenderingHits.KEY\u插值,RenderingHits.VALUE\u插值\u双线性); g2.drawImage(已调整大小,0,0,currentW,currentH,null); g2.dispose(); 调整大小=温度; } 返回调整大小; }否则{ 对象提示=比例>2?RenderingHits.VALUE\u INTERPOLATION\u双三次:RenderingHits.VALUE\u INTERPOLATION\u双线性; BuffereImage resized=新的BuffereImage(w,h,BuffereImage.TYPE_INT_ARGB); Graphics2D g2=已调整大小。createGraphics(); g2.setRenderingHint(RenderingHints.KEY\u插值,hint); g2.drawImage(图像,0,0,w,h,空); g2.dispose(); 返回调整大小; } },java,image,resize,Java,Image,Resize,只需使用Burkhard的答案,但在创建图形后添加这一行: g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 您还可以将该值设置为双三次,这样可以生成质量更好的图像,但操作成本更高。您还可以设置其他渲染提示,但我发现插值会产生最显著的效果。 请记住,如果您想放大很多,java代码很可能会非常慢。我发现较大的图像开始产生大约300%的缩放延迟

只需使用Burkhard的答案,但在创建图形后添加这一行:

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
您还可以将该值设置为双三次,这样可以生成质量更好的图像,但操作成本更高。您还可以设置其他渲染提示,但我发现插值会产生最显著的效果。
请记住,如果您想放大很多,java代码很可能会非常慢。我发现较大的图像开始产生大约300%的缩放延迟,即使所有渲染提示都设置为优化速度而不是质量。

事实证明,编写性能缩放器并非易事。我为一个开源项目做过一次:


原则上,“java.awt.Image#getScaledInstance(int,int,int)”也可以完成这项工作,但它存在一个严重的错误-有关详细信息,请参阅我的链接。

尝试以下方法:

ImageIcon icon = new ImageIcon("image.png");
Image img = icon.getImage();
Image newImg = img.getScaledInstance(350, 350, java.evt.Image.SCALE_SMOOTH);
icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, "image on The frame", "Display Image", JOptionPane.INFORMATION_MESSAGE, icon);

我发现这种技术创建的图像质量不够高,无法满足我的需要。will image.getScaledInstance(宽度、高度、提示)也可以吗?preserveAlpha检查方法是否错误(对于图像类型)?我同意@morgancodes。图像质量比您在调整到相同尺寸时使用例如OSX预览所获得的图像质量差得多。将尝试一些开源库,看看它们是否更好。“将尝试一些开源库,看看它们是否更好。”@RiyadKalla的答案中imgscalr的+1。既然您要求一个库,真正的答案是:。它比公认答案中的代码更容易使用;)我不同意库部分:Graphics2D是awt库的一部分,并且是开源的。对于最后一部分(开源),我不是100%确定,但谁会看awt代码呢?Chris的文章正是促使我首先编写imgscalr的原因;还有像这样的问题(还有像你这样的答案)。很多人一次又一次地问如何从图像中获得好看的缩略图。有很多方法可以做到这一点,克里斯的方法并不总是最好的方法,这取决于你想做什么,以及减少的幅度有多大。IGGSCAR解决了所有这些问题,它是1类。+ 1,我同意,Filthy Rich客户端是最好的java图书之一,与“有效java”相称,但ImgScalr是我使用的,因为我是懒惰的,库柏,非常好的API工作。非常直接且易于使用。@LordT:我很高兴您发现Thumbnailator很容易使用:)我如何创建带有500x400原始图像的80x80缩略图?我看不出有任何选择。谢谢“.outputFormat(“JPEG”)”在文档中没有写过。@Vincent可以使用的方法的完整列表在Thumbnailator API文档中——Riyad,我喜欢使用Scalr。我很好奇地想知道,您最终是如何选择一个包含所有静态方法的API的?我编写了一个类似的api,它更接近于一个构建器。类似于
新的ImageScaler(img).重新调整大小(…).旋转(…).旋转(…).cropTo(…).toOutputBuffer()
。我也喜欢你的方法,而且我认为它更简单。Amir,builder模式对于这些类型的库也非常有效,我只是碰巧选择了静态方法,因为我认为它对于新用户来说更容易遵循,并且使用模式也是我从ImgCalr(orig)期望的
Thumbnails.of("path/to/image")
    .size(100, 100)
    .toFile("path/to/thumbnail");
Thumbnails.of(new File("path/to/directory").listFiles())
    .size(100, 100)
    .outputFormat("JPEG")
    .outputQuality(0.85)
    .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
<dependency>
    <groupId>com.mortennobel</groupId>
    <artifactId>java-image-scaling</artifactId>
    <version>0.8.6</version>
</dependency>
ResampleOp resamOp = new ResampleOp(50, 40);
BufferedImage modifiedImage = resamOp.filter(originalBufferedImage, null);
 /**
     * utility method to get an icon from the resources of this class
     * @param name the name of the icon
     * @return the icon, or null if the icon wasn't found.
     */
    public Icon getIcon(String name) {
        Icon icon = null;
        URL url = null;
        ImageIcon imgicon = null;
        BufferedImage scaledImage = null;
        try {
            url = getClass().getResource(name);

            icon = new ImageIcon(url);
            if (icon == null) {
                System.out.println("Couldn't find " + url);
            }

            BufferedImage bi = new BufferedImage(
                    icon.getIconWidth(),
                    icon.getIconHeight(),
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = bi.createGraphics();
            // paint the Icon to the BufferedImage.
            icon.paintIcon(null, g, 0,0);
            g.dispose();

            bi = resizeImage(bi,30,30);
            scaledImage = bi;// or replace with this line Scalr.resize(bi, 30,30);
            imgicon = new ImageIcon(scaledImage);

        } catch (Exception e) {
            System.out.println("Couldn't find " + getClass().getName() + "/" + name);
            e.printStackTrace();
        }
        return imgicon;
    }

 public static BufferedImage resizeImage (BufferedImage image, int areaWidth, int areaHeight) {
        float scaleX = (float) areaWidth / image.getWidth();
        float scaleY = (float) areaHeight / image.getHeight();
        float scale = Math.min(scaleX, scaleY);
        int w = Math.round(image.getWidth() * scale);
        int h = Math.round(image.getHeight() * scale);

        int type = image.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;

        boolean scaleDown = scale < 1;

        if (scaleDown) {
            // multi-pass bilinear div 2
            int currentW = image.getWidth();
            int currentH = image.getHeight();
            BufferedImage resized = image;
            while (currentW > w || currentH > h) {
                currentW = Math.max(w, currentW / 2);
                currentH = Math.max(h, currentH / 2);

                BufferedImage temp = new BufferedImage(currentW, currentH, type);
                Graphics2D g2 = temp.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2.drawImage(resized, 0, 0, currentW, currentH, null);
                g2.dispose();
                resized = temp;
            }
            return resized;
        } else {
            Object hint = scale > 2 ? RenderingHints.VALUE_INTERPOLATION_BICUBIC : RenderingHints.VALUE_INTERPOLATION_BILINEAR;

            BufferedImage resized = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = resized.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
            g2.drawImage(image, 0, 0, w, h, null);
            g2.dispose();
            return resized;
        }
    }
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
ImageIcon icon = new ImageIcon("image.png");
Image img = icon.getImage();
Image newImg = img.getScaledInstance(350, 350, java.evt.Image.SCALE_SMOOTH);
icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, "image on The frame", "Display Image", JOptionPane.INFORMATION_MESSAGE, icon);