图像调整大小并在JPanel或JLabel中显示而不会降低质量 [英] Image resizing and displaying in a JPanel or a JLabel without loss of quality
问题描述
我正在开发一个java程序,以便在注册时使用网络摄像头捕获员工图像。我可以毫无问题地获得图片,并将其保存在我的C:驱动器中,但在检索图像时,只有部分图像显示在标签上。有没有办法在保存之前重新调整JPEG大小?还是在显示之前?喜欢收缩它没有质量损失....
非常感谢
Cheerz!
:)!
好的家伙......这里有: - 我用我使用过的方式对代码进行了评论。
//此方法将从界面捕获图像并将其保存在唯一的员工ID
public String captureImage(int picId){
FrameGrabbingControl ControlFG =(FrameGrabbingControl)
broadcast.getControl(javax.media.control.FrameGrabbingControl);
缓冲区缓冲区= ControlFG.grabFrame();
BufferToImage image = new BufferToImage((VideoFormat)buffer.getFormat());
img = image.createImage(buffer);
path =c:\\employee+ picId +。jpg;
saveJPG(img,path); //方法将保存图像
返回路径;
}
public void saveJPG(Image img,String s){*** //方法将保存图像***
系统.out.println(一个或多个);
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(找不到文件);
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f,false);
encoder.setJPEGEncodeParam(param);
尝试
{
encoder.encode(bi);
out.close();
}
catch(java.io.IOException io)
{
System.out.println(IOException);
}
}
也许我可以在保存时缩放图像..所以我可以检索缩放的图像..
当然你可以通过很多不同的方式调整图像大小,例如
I'm developing a java program to capture employee images at the time of registration using a webcam. I can obtain the picture without any problem, and save it in my C: drive but upon retrieval of the image only a part of the image is displayed on the label. Is there a way of re sizing the JPEG before saving it? or before displaying it? like shrinking it without a quality loss....
Thankz a lot Cheerz! :)!
okay guys... here goes:- I have commented the code in the way I have used them.
//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");
}
}
maybe I can scale the image while saving.. so that I can retrieve the scaled image..
Of course you can resize the image there are many different ways like Image#getScaledInstance(int width,int height,int hints)
, but this has its perils.
The main problem being:
Image.getScaledInstance() does not return a finished, scaled image. It leaves much of the scaling work for a later time when the image pixels are used.
I would not recommend using it but here is a nice example.
Alternatively you can use this method:
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Color;
import java.awt.Graphics2D;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.RenderingHints;
public class ImgUtils {
public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
BufferedImage bi = null;
try {
ImageIcon ii = new ImageIcon(filename);//path to image
bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bi;
}
}
you'd use it like:
final BufferedImage img=new ImgUtils().scaleImage(200,200,"c:/test.jpg");
//create label with image as background
JLabel label=new JLabel(new ImageIcon((Image)img));
UPDATE:
Here is a small example I made:
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class JavaApplication117 {
//change this to your own
static String filename="c:/test.jpg";
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JavaApplication117().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
private void initComponents(JFrame frame) {
final BufferedImage img = new ImgUtils().scaleImage(200, 200, filename);
//create label with image as background
JLabel label = new JLabel(new ImageIcon((Image) img));
frame.getContentPane().add(label, BorderLayout.CENTER);
}
}
class ImgUtils {
public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
BufferedImage bi = null;
try {
ImageIcon ii = new ImageIcon(filename);//path to image
bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bi;
}
}
References:
这篇关于图像调整大小并在JPanel或JLabel中显示而不会降低质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!