调整后的图像质量非常低 - Java [英] Quality of Image after resize very low -- Java

查看:116
本文介绍了调整后的图像质量非常低 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在脚本中,它从300x300左右的标记下降到60x60。需要提高整体图像质量,因为它目前非常糟糕。

  public static Boolean resizeImage(String sourceImg, String destImg,Integer Width,Integer Height,Integer whiteSpaceAmount)
{
BufferedImage origImage;

尝试
{
origImage = ImageIO.read(new File(sourceImg));
int type = origImage.getType()== 0? BufferedImage.TYPE_INT_ARGB:origImage.getType();
int fHeight =高度;
int fWidth =宽度;
int whiteSpace = Height + whiteSpaceAmount; //将所有格式设置为正方形,因此不需要两个whiteSpace计算器。
double aspectRatio;

//计算尺寸调整后的尺寸
if(origImage.getHeight()> origImage.getWidth())//如果图片高度大于宽度,则适当缩放。
{
fHeight =身高; //将高度设置为60,因为它是最大的一面。

aspectRatio =(double)origImage.getWidth()/(double)origImage.getHeight(); //获取图片的宽高比。
fWidth =(int)Math.round(Width * aspectRatio); //设置通过宽高比创建的宽度。
}
else if(origImage.getHeight()< origImage.getWidth())//如果图片宽度大于适当的高度比例。
{
fWidth =宽度; //将高度设置为60,因为它是最大的一面。

aspectRatio =(double)origImage.getHeight()/(double)origImage.getWidth(); //获取图片的宽高比。
fHeight =(int)Math.round(Height * aspectRatio); //设置通过纵横比创建的高度。
}

int extraHeight = whiteSpace - fHeight;
int extraWidth = whiteSpace - fWidth;

BufferedImage resizedImage = new BufferedImage(whiteSpace,whiteSpace,type);
Graphics2D g = resizedImage.createGraphics();
g.setColor(Color.white);
g.fillRect(0,0,whiteSpace,whiteSpace);

g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

g.drawImage(origImage,extraWidth / 2,extraHeight / 2,fWidth,fHeight,null);
g.dispose();

ImageIO.write(resizedImage,jpg,new File(destImg));
}
catch(IOException ex)
{
return false;
}

返回true;
}

真的只需要知道他们是否可插入的东西会碰撞提高质量或者我需要完全看看别的东西。



编辑:图片比较。



来源,刚刚从谷歌挑选一台随机洗衣机。





什么它看起来像这样转换。
http://imgur.com/8WlXD



解决方案

您看到的问题实际上与用于缩减的重采样过滤器有关。显然,你的图书馆使用的那个是糟糕的情况。最小邻居,双线性和双三次是降尺度时使用的典型坏例子。我不知道Photoshop使用的确切重采样过滤器,但我使用3瓣lanczos并得到以下结果:





因此,要解决您的问题,您需要使用更智能的重采样过滤器。


In the script it is going from around the 300x300 mark down to 60x60. Need to improve the overall image quality as it is coming out very poorly at the moment.

public static Boolean resizeImage(String sourceImg, String destImg, Integer Width, Integer Height, Integer whiteSpaceAmount) 
{
    BufferedImage origImage;

    try 
    {
        origImage = ImageIO.read(new File(sourceImg));
        int type = origImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : origImage.getType();
        int fHeight = Height;
        int fWidth = Width;
        int whiteSpace = Height + whiteSpaceAmount; //Formatting all to squares so don't need two whiteSpace calcs..
        double aspectRatio;

        //Work out the resized dimensions
        if (origImage.getHeight() > origImage.getWidth()) //If the pictures height is greater than the width then scale appropriately.
        {
            fHeight = Height; //Set the height to 60 as it is the biggest side.

            aspectRatio = (double)origImage.getWidth() / (double)origImage.getHeight(); //Get the aspect ratio of the picture.
            fWidth = (int)Math.round(Width * aspectRatio); //Sets the width as created via the aspect ratio.
        }
        else if (origImage.getHeight() < origImage.getWidth()) //If the pictures width is greater than the height scale appropriately.
        {
            fWidth = Width; //Set the height to 60 as it is the biggest side.

            aspectRatio = (double)origImage.getHeight() / (double)origImage.getWidth(); //Get the aspect ratio of the picture.
            fHeight = (int)Math.round(Height * aspectRatio); //Sets the height as created via the aspect ratio.
        }

        int extraHeight = whiteSpace - fHeight;
        int extraWidth = whiteSpace - fWidth;

        BufferedImage resizedImage = new BufferedImage(whiteSpace, whiteSpace, type);
        Graphics2D g = resizedImage.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, whiteSpace, whiteSpace);

        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.drawImage(origImage, extraWidth/2, extraHeight/2, fWidth, fHeight, null);
        g.dispose();

        ImageIO.write(resizedImage, "jpg", new File(destImg));
    } 
    catch (IOException ex) 
    {
        return false;
    }

    return true;
}

Really just need to know if their is something I can plug in that will bump up the quality or if I need to look at something else entirely.

EDIT: Picture comparison.

Source, just picked a random washing machine from google. http://www.essexappliances.co.uk/images/categories/washing-machine.jpg

The same picture converted in Photoshop to what I need it to be. http://imgur.com/78B1p

What it looks like being converted like this. http://imgur.com/8WlXD

解决方案

The issue you are seeing is actually related to the resampling filter used for downscaling. Obviously, the one used by your library is a bad one for the situation. Nearest neighbor, bilinear and bicubic are typical bad examples to be used when downscaling. I don't know the exact resampling filter Photoshop uses, but I used 3-lobed lanczos and got the following result:

So, to solve your problem, you need to use a smarter resampling filter.

这篇关于调整后的图像质量非常低 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆