gpt4 book ai didi

java - 在不损失质量的情况下在 JPanel 或 JLabel 中调整图像大小和显示

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:35 25 4
gpt4 key购买 nike

我正在开发一个 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");
}
}

也许我可以在保存时缩放图像..这样我就可以检索缩放后的图像..

最佳答案

当然你可以调整图像的大小,有很多不同的方法,比如 Image#getScaledInstance(int width,int height,int hints) , 但是 this has its perils .

主要问题是:

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.

我不推荐使用它,但 here是一个很好的例子。

或者你可以使用这个方法:

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;
}

}

你会像这样使用它:

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));

更新:

这是我做的一个小例子:

enter image description here

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;
}
}

引用资料:

关于java - 在不损失质量的情况下在 JPanel 或 JLabel 中调整图像大小和显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12660122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com