我正在开发一个 java 程序,用于在注册时使用网络摄像头捕获员工图像。我可以毫无问题地获得图片,并将其保存在我的 C: 驱动器中,但在检索图像时,标签上仅显示图像的一部分。有没有办法在保存之前调整 JPEG 的大小?还是在显示之前?就像在没有质量损失的情况下缩小它......
非常感谢Cheerz!:)!
好吧,伙计们......这里是:-我已经以我使用它们的方式评论了代码。
//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");
}
}
也许我可以在保存时缩放图像.. 这样我就可以检索缩放后的图像..