📄 imagecapture.java
字号:
package video;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageCapture extends JFrame {
int xpoint = 40, ypoint = 40;
private Image image;
int width,hight;
JButton jb=new JButton("save");
JTextField jtf=new JTextField(30);
String fileAddr;
public ImageCapture(Image img) {
super("ImageCapture");
this.image=img;
width=image.getWidth(null);
hight=image.getHeight(null);
setLayout(new BorderLayout());
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(1,2));
jp.add(jb);
jp.add(jtf);
jb.addActionListener(jbEvent);
add(BorderLayout.SOUTH,jp);
pack();
setSize(400, 400);
setVisible(true);
}
JB jbEvent=new JB();
class JB implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(jtf.getText()==null){
fileAddr="d://image.jpg";
}else{
fileAddr=jtf.getText();
}
BufferedImage bi = (BufferedImage) createImage(width,hight);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(image, null, null);
FileOutputStream out = null;
try
{
File f = new File(fileAddr);
out = new FileOutputStream(f);
}
catch (java.io.FileNotFoundException io)
{
System.out.println("File Not Found");
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1f, false);//不压缩图像
encoder.setJPEGEncodeParam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.IOException io)
{
System.out.println("IOException");
}
}
}
public void paint(Graphics g) {
System.out.println(g);
g.drawImage(image,xpoint,ypoint,this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -