java Java如何设置jpg质量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13204432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:52:09  来源:igfitidea点击:

Java how to set jpg quality

java

提问by NestedCodeblocksFTW

Just wanting to get some code edited so that the output jpg quality isn't the default low quality setting that

只是想编辑一些代码,以便输出 jpg 质量不是默认的低质量设置

try
        {
            ImageIO.write(var6, "jpg", var7);
        }

.....is using currently.

.....目前正在使用。

I've looked at some other java examples of setting quality, not being very familiar with Java I'm having trouble understanding how to plug stuff in and rework some examples, that I've seen on using Java to set image quality.

我查看了其他一些设置质量的 Java 示例,但对 Java 不是很熟悉,我无法理解如何插入内容并重新编写一些示例,这些示例是我在使用 Java 设置图像质量时看到的。

ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());
    iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwparam.setCompressionQuality(quality);
    writer.write(null, new IIOImage(image, null, null), iwparam);

Here is the code I'm trying to get work........

这是我正在努力工作的代码........

public static String func_74292_a(File par0File, String par1Str, int par2, int par3)
{
    File var4 = new File(par0File, "screenshots");
    var4.mkdir();
    int var5 = par2 * par3;

    if (field_74293_b == null || field_74293_b.capacity() < var5)
    {
        field_74293_b = BufferUtils.createIntBuffer(var5);
        field_74294_c = new int[var5];
    }

    GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
    field_74293_b.clear();
    GL11.glReadPixels(0, 0, par2, par3, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, field_74293_b);
    field_74293_b.get(field_74294_c);
    func_74289_a(field_74294_c, par2, par3);
    BufferedImage var6 = new BufferedImage(par2, par3, 1);
    var6.setRGB(0, 0, par2, par3, field_74294_c, 0, par2);

    if (par1Str == null)
    {
        var7 = func_74290_a(var4);
    }
    else
    {
        var7 = new File(var4, par1Str);
    }

    try
    {
        ImageIO.write(var6, "jpg", var7);
    }
    catch (IOException var8)
    {
        ;
    }

    Thread var7x = new Thread(new ScreenShotHelper());
    var7x.start();
    return "\u00a7aUploading Screenshot....";
}

private static File func_74290_a(File par0File)
{
    String var1 = dateFormat.format(new Date()).toString();
    int var2 = 1;

    while (true)
    {
        File var3 = new File(par0File, var1 + (var2 == 1 ? "" : "_" + var2) + ".jpg");

        if (!var3.exists())
        {
            return var3;
        }

        ++var2;
    }
}

回答by NestedCodeblocksFTW

Finally did it with this code ...

终于用这个代码做到了......

try
{

    ImageOutputStream  ios =  ImageIO.createImageOutputStream(var7);
    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter writer = iter.next();
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(0.85f);
    writer.setOutput(ios);
    writer.write(null, new IIOImage(var6,null,null),iwp);
    writer.dispose();

    //ImageIO.write(var6, "jpg", var7);
}

回答by sarcan

You might want to elaborate on what your actual problem with the code is.

您可能想详细说明代码的实际问题是什么。

Generally speaking, the second sniplet you were using is (more or less) the correct approach:

一般来说,您使用的第二个 sniplet 是(或多或少)正确的方法:

1) ImageIO.write(...) uses default values for pretty much everything, it requires no extra configuration.

1) ImageIO.write(...) 几乎所有东西都使用默认值,它不需要额外的配置。

2) If you want to tweak parameters, e.g. for the compression ratio, you should instead use an ImageWriter. You can obtain a suitable writer for any format (in your case jpg) using ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg");

2) 如果您想调整参数,例如压缩比,您应该使用 ImageWriter。您可以使用ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg");

3) You then set the configuration parameters to be used by the writer on an instance of ImageWriteParam. You could instanciate a JPEGImageWriteParamdirectly, but if you're just looking to change the compression ratio it is easier to request a default instance using ImageWriteParam param = writer.getDefaultWriteParam();

3) 然后在 ImageWriteParam 的实例上设置编写器要使用的配置参数。您可以JPEGImageWriteParam直接实例化 a ,但如果您只是想更改压缩率,则使用以下命令更容易请求默认实例ImageWriteParam param = writer.getDefaultWriteParam();

4) Set the compression quality as shown in the above code snipplet, and set the compression type to explicit accordingly.

4) 如上代码片段所示设置压缩质量,并相应地将压缩类型设置为显式。

5) The call to writer.write(null, new IIOImage(image, null, null), iwparam);basically tells your writer instance to create an image without meta data or embedded thumbnails, containing nothing but your BufferedImage and using the configuration object you created in 3).

5) 调用writer.write(null, new IIOImage(image, null, null), iwparam);基本上告诉您的编写器实例创建一个没有元数据或嵌入缩略图的图像,只包含您的 BufferedImage 并使用您在 3) 中创建的配置对象。

回答by Hoku

I came across a similar problem and the answer was not very clear to me, since at the time i did not have a knowledge on ImageIO, so to the people like me that came across this post i made a example.

我遇到了一个类似的问题,答案对我来说不是很清楚,因为当时我对 ImageIO 一无所知,所以对于像我这样遇到这篇文章的人,我举了一个例子。

try {

            //  Image to be altered 
            BufferedImage imagem = ImageIO.read(new File("c://nota.jpg"));

            //  The output image
            File outPutImage = new File("c://nota2.jpg");

            //  Encapsulate the outPut image
            ImageOutputStream  ios =  ImageIO.createImageOutputStream(outPutImage);

            //  List of ImageWritre's for jpeg format 
            Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");

            //  Capture the first ImageWriter
            ImageWriter writer = iter.next();

            //  define the o outPut file to the write
            writer.setOutput(ios);

            //  Here you define the changes you wanna make to the image
            ImageWriteParam iwParam = writer.getDefaultWriteParam();
            iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            iwParam.setCompressionQuality(.90f);

            //  Compression, etc... being made
            writer.write(null, new IIOImage(imagem, null, null), iwParam);

            writer.dispose();

            //  Write to altered image in memory to the final file
            ImageIO.write(imagem, "jpg", ios);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

If you know of a easier way or you found out something wrong in my comments or code, please let me know in the comments so i can alter.

如果您知道更简单的方法,或者您发现我的评论或代码中有问题,请在评论中告诉我,以便我进行更改。