📄 webcang.java
字号:
import javax.swing.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
public class WebCang extends JApplet implements ActionListener
{
//捕获媒体数据需要做以下工作:
//1.定位所需要用的捕获设备,可以通过查询CaptureDeviceManager来定位
public static Player player = null; // 定义第一个视频图像播放器
public static Player player2 = null; // 定义第二个频播放器
private CaptureDeviceInfo di = null; // 获取第一个视频设备
private MediaLocator ml = null;
private CaptureDeviceInfo di2 = null; // 获取第二个视频设备
private MediaLocator ml2 = null;
private JButton capture = null; //拍照
private JButton save = null; //保存
private JTextField num = null;
private Buffer buf = null;
private Image img = null;
//private VideoFormat vf = null;
private BufferToImage btoi = null;
private ImagePanel imgpanel = null; //用来显示图像的panel
private JButton capture2 = null; //拍照
private JButton save2 = null; //保存
private JTextField num2 = null;
private Buffer buf2 = null;
private Image img2 = null;
//private VideoFormat vf2 = null;
private BufferToImage btoi2 = null;
private ImagePanel imgpanel2 = null;
private int rectX;
private int rectY;
private int rectWidth = 300;
private int rectHeight = 450;
private int imgWidth = 320;
private int imgHeight = 240;
private String fname = "test";
public WebCang()
{
this.setLayout(new BorderLayout());
setSize(320, 550);
//2.获取这个捕获设备的信息CaptureDeviceInfo对象
imgpanel = new ImagePanel();
imgpanel.addMouseMotionListener(imgpanel);
capture = new JButton("拍照1"); //拍照按钮
capture.addActionListener(this);
save = new JButton("保存1"); //保存按钮
save.addActionListener(this);
num = new JTextField(); //路径输入框
imgpanel2 = new ImagePanel();
//imgpanel2.addMouseMotionListener(imgpanel);
capture2 = new JButton("拍照2"); //拍照按钮
//capture.addActionListener(this);
save2 = new JButton("保存2"); //保存按钮
//save.addActionListener(this);
num2 = new JTextField(); //路径输入框
//String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";// 设置摄像头驱动类型
//3.从CaptureDeviceInfo对象中获取捕获设备的位置Medialocator。
di = CaptureDeviceManager.getDevice(str2);
//System.out.println("dddd:"+di);
//4.利用MediaLocator创建DataSource。
ml = di.getLocator();
java.util.Vector video_devices = CaptureDeviceManager.getDeviceList(new VideoFormat(VideoFormat.YUV)); //RGB YUV
for (int i = 0; i < video_devices.size(); i++)
{
di2 = (CaptureDeviceInfo)video_devices.get(i);
}
ml2 = di2.getLocator();
try
{
//5.使用DataSource创建Player或是Processor。
player = Manager.createRealizedPlayer(ml);
//6.然后启动Player就开始了媒体的捕获。
player.start();
Component comp;
if ((comp = player.getVisualComponent()) != null)
{
add(comp, BorderLayout.NORTH);
}
Panel panel1 = new Panel();
panel1.setLayout(new GridLayout(4,4));
panel1.add(capture, BorderLayout.NORTH);
System.out.println("cc");
panel1.add(new Label("请输入对应的文件名1:"), BorderLayout.WEST);
panel1.add(num, BorderLayout.CENTER);
panel1.add(save, BorderLayout.SOUTH);
this.add(panel1, BorderLayout.CENTER);
this.add(imgpanel, BorderLayout.SOUTH);
}
catch (Exception e)
{
System.out.println("实例化第一个摄像头时出错!"+e);
}
/*try
{
//5.使用DataSource创建Player或是Processor。
player2 = Manager.createRealizedPlayer(ml2);
//6.然后启动Player就开始了媒体的捕获。
player2.start();
Component comp2;
if ((comp2 = player2.getVisualComponent()) != null)
{
add(comp2, BorderLayout.NORTH);
}
System.out.println("dd");
Panel panel2 = new Panel(new BorderLayout());
panel2.add(capture2, BorderLayout.NORTH);
panel2.add(new Label("请输入对应的文件名2:"), BorderLayout.WEST);
panel2.add(num2, BorderLayout.CENTER);
panel2.add(save2, BorderLayout.SOUTH);
this.add(panel2, BorderLayout.CENTER);
this.add(imgpanel2, BorderLayout.SOUTH);
}
catch (Exception e2)
{
System.out.println("实例化第二个摄像头时时出错!"+e2);
}*/
}
public static void main(String[] args)
{
JFrame f = new JFrame("开始");
WebCang cf = new WebCang();
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
playerclose();
System.exit(0);
}
});
f.add("Center", cf);
f.pack();
f.setSize(new Dimension(320,590));
f.setVisible(true);
WebCang2();
Runtime.getRuntime.exec("java WebCang2");
}
public static void playerclose()
{
player.close();
player.deallocate();
}
public void actionPerformed(ActionEvent e)
{
JComponent c = (JComponent) e.getSource();
if (c == capture)//当点击了拍照按钮
{
FrameGrabbingControl fgc = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
//buf = fgc.grabFrame(); 防止图像的Frame还没有获取,就执行了fgc.grabFrame()。
while((buf=fgc.grabFrame()).getLength() == 0)
{
buf = fgc.grabFrame(); //获取当前帧并存入Buffer类
System.out.println("length:" + buf.getLength());
try
{
Thread.sleep(1000);
}
catch (InterruptedException e_buf)
{
e_buf.printStackTrace();
}
}
btoi = new BufferToImage((VideoFormat)buf.getFormat());
//
img = btoi.createImage(buf); // 把图片写入到缓存中,并显示图片
imgpanel.setImage(img); // save image
}
else if (c == save)
{
if (img != null)
{
fname = !num.getText().equals("") ? num.getText() : "test";
saveJPG(img, "D:/Photo/" + fname + ".jpg");
}
}
}
class ImagePanel extends Panel implements MouseMotionListener
{
private Image myimg = null;
public ImagePanel()
{
setLayout(null);
setSize(imgWidth, imgHeight);
}
public void setImage(Image img)
{
this.myimg = img;
repaint();
}
public void update(Graphics g)
{
g.clearRect(0, 0, getWidth(), getHeight());
if (myimg != null)
{
g.drawImage(myimg, 0, 0, this);
g.setColor(Color.RED);
g.drawRect(rectX, rectY, rectWidth, rectHeight);
}
}
public void paint(Graphics g)
{
update(g);
}
public void mouseDragged(MouseEvent e)
{
rectX = e.getX() - 50;
rectY = e.getY() - 50;
repaint();
}
public void mouseMoved(MouseEvent e)
{
}
}
public void saveJPG(Image img, String s)
{
/*BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight);*/
BufferedImage bi = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.clipRect(rectX, rectY, rectWidth, rectHeight);
g2.drawImage(img, null, null);
int moveX = rectX > 0 ? rectX : 0;
int moveY = rectY > 0 ? rectY : 0;
int cutWidth =
rectX + rectWidth > imgWidth
? rectWidth - ((rectX + rectWidth) - imgWidth)
: rectWidth;
int cutHeight =
rectY + rectHeight > imgHeight
? rectHeight - ((rectY + rectHeight) - imgHeight)
: rectHeight;
bi = bi.getSubimage(moveX, moveY, cutWidth, cutHeight);
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(1f, false);
encoder.setJPEGEncodeParam(param);
try
{
encoder.encode(bi);
out.close();
JOptionPane.showMessageDialog(null,"图片保存成功");//弹出消息框
}
catch (java.io.IOException io)
{
System.out.println("拍照图片保存出错!"+io);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -