创建JPEG文件:转换访问限制code(JPEGImageEn codeR)至"安全" code [英] Creating JPEGs: converting access restricted code (JPEGImageEncoder) to "safe" code

查看:202
本文介绍了创建JPEG文件:转换访问限制code(JPEGImageEn codeR)至"安全" code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到以下code,和Eclipse标志着它(在 JPEGImageEn codeR 线)错误(访问限制)。我改变了Eclipse的选项,以使该code编译,但我读到的错误意味着这个类( JPEGImageEn codeR )可能不会被一些JRE实现实现(未在Sun / Oracle的一个)。

那么,究竟应该是code,它不会有准入限制,即完全安全的code做同样的事情(创建一个JPG图片)?

 的BufferedOutputStream出=新的BufferedOutputStream(新的FileOutputStream(文件名));
JPEGImageEn codeR EN codeR = JPEG codec.createJPEGEn codeR(出);
JPEGEn codeParam参数= EN coder.getDefaultJPEGEn codeParam(buffImage);
param.setQuality(0.8f,FALSE);
EN coder.en code(buffImage,参数);


解决方案

也许我误解了,但如果你希望做一个BufferedImage对象保存为JPEG格式,你可以这样(从Java 1.4起做):

  ImageIO.write(BufferedImage的,JPG,文件);

下面是包含更多信息的链接: http://download.oracle .COM / JavaSE的/教程/ 2D /图像/ saveimage.html

正如你所看到的,它说,JPEG,PNG,GIF,BMP和WBMP会一直支持。

如果你想设置COM pression /质量,这是多一点的工作,但不会太大。假设你有一个BufferedImage的和不过outFile:

  IIOImage中outputImage =新方法IIOImage(BufferedImage的,NULL,NULL);ImageWriter的作家= ImageIO.getImageWritersByFormatName(JPEG)下一个()。
writer.setOutput(新FileImageOutputStream(不过outFile));
的ImageWriteParam writeParam = writer.getDefaultWriteParam();
writeParam.setCom pressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCom pressionQuality(.75f); //浮动0和1之间,1表示最大质量。
writer.write(NULL,outputImage,writeParam);

(由previous答案固定)

I was given the following code, and Eclipse marked it (at the JPEGImageEncoder line) as an error (Access restriction). I changed Eclipse options to make that code compile, but I read that the error means that that class (JPEGImageEncoder) may not be implemented by some JRE implementation (not a Sun/Oracle one).

So, what should be the code that wouldn't have access restrictions, i.e. completely safe code to do the same thing (create a JPG image)?

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buffImage);
param.setQuality(0.8f, false);
encoder.encode(buffImage, param);

解决方案

Maybe I've misunderstood, but if all you're looking to do is save a BufferedImage object as a jpeg, you can do this (from Java 1.4 onwards):

ImageIO.write(bufferedImage,"jpg",file);

Here's a link with more information: http://download.oracle.com/javase/tutorial/2d/images/saveimage.html

As you can see, it says that JPEG, PNG, GIF, BMP and WBMP will always be supported.

If you want to set the compression/quality, it's a little more work but not too much. Assuming you have a bufferedImage and an outFile:

IIOImage outputImage = new IIOImage(bufferedImage, null, null);

ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();        
writer.setOutput(new FileImageOutputStream(outFile));
ImageWriteParam writeParam = writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionQuality(.75f); // float between 0 and 1, 1 for max quality.
writer.write( null, outputImage, writeParam);

(fixed from previous answer)

这篇关于创建JPEG文件:转换访问限制code(JPEGImageEn codeR)至"安全" code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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