【发布时间】:2012-09-21 12:37:10
【问题描述】:
我正在开发一个 java 程序,用于在注册时使用网络摄像头捕获员工图像。我可以毫无问题地获取图片,并将其保存在我的 C: 驱动器中,但在检索图像时,标签上仅显示部分图像。有没有办法在保存之前调整 JPEG 的大小?还是在显示之前?就像在没有质量损失的情况下缩小它......
非常感谢 干杯! :)!
好的,伙计们...这里是:- 我已经按照我使用它们的方式注释了代码。
//This method will capture the image from the interface and save it under the unique employee ID
public String captureImage(int picId){
FrameGrabbingControl ControlFG = (FrameGrabbingControl)
broadcast.getControl("javax.media.control.FrameGrabbingControl");
Buffer buffer = ControlFG.grabFrame();
BufferToImage image = new BufferToImage((VideoFormat)buffer.getFormat());
img = image.createImage(buffer);
path="c:\\employee"+picId+".jpg";
saveJPG(img,path);//method will save the image
return path;
}
public void saveJPG(Image img, String s){***//method will save the image***
System.out.println(s);
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img,null,null);
FileOutputStream out = null;
try{
out = new FileOutputStream(s);
}
catch (java.io.FileNotFoundException io){
System.out.println("File Not Found");
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f,false);
encoder.setJPEGEncodeParam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.IOException io)
{
System.out.println("IOException");
}
}
也许我可以在保存时缩放图像.. 这样我就可以检索缩放后的图像..
【问题讨论】:
-
“在不损失质量的情况下缩小它” 这是不可能的。各种平滑技术可以部分隐藏图像调整大小的伪影,但以 WxH 为单位降低分辨率,图像“质量”必须更低。
-
@AndrewThompson 是的,这是真的......质量下降是可以的,但整个图像没有显示......到目前为止,我只能获得缩小图像的一部分......
-
如需尽快获得更好的帮助,请发帖 SSCCE。也许热链接到these images 之一。
-
为了尽快获得更好的帮助(除了提供SSCCE,正如@AndrewThompson 已经提到的那样)尝试通过a)学习并坚持Java命名约定b)完全(并且始终如一地)使阅读您的问题变得愉快! ) 格式化代码(有一个帮助按钮,点击时会显示操作方法:-)
-
如果您真的对质量感兴趣,没有任何办法可以阅读 graphics2D 选项的基础知识,f.i.早期的博客today.java.net/pub/a/today/2007/04/03/… 或 filtyRichClients
标签: java swing jpanel jpeg jlabel