import javax.media.Player;
import javax.media.CaptureDeviceInfo;
import javax.media.MediaLocator;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.media.control.FrameGrabbingControl;
import javax.media.Buffer;
import javax.media.util.BufferToImage;
import javax.media.format.VideoFormat;
import java.io.*;
import com.sun.image.codec.jpeg.*;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import java.applet.*;
public class Camera extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*
*/
private static Player player = null;
private CaptureDeviceInfo device = null;
private MediaLocator locator = null;
private Buffer buffer = null;
private BufferToImage b2i = null;
private Image image;
private ImageIcon iicon = new ImageIcon();
boolean proportion = true;
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
Component component1;
JLabel jLabel1 = new JLabel();
public Camera(){
super("我的摄像头");
try{
jbInit();
}catch (Exception e){e.printStackTrace();};
}
/* public Image resize(int width,int height,Image source,boolean flag){
this.proportion = flag;
int new_w;
int new_h;
Toolkit tk = Toolkit.getDefaultToolkit();
Applet app = new Applet();
MediaTracker mt = new MediaTracker(app);
Image img = source;
try {
mt.addImage(img, 0);
mt.waitForID(0);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(img.getWidth(null)!=-1){
System.out.println("can't read,retry!");
return null;
}else{
if(this.proportion==true){
double rate1 = ((double)img.getWidth(null))/(double)width+0.1;
double rate2 = ((double)img.getHeight(null))/(double)height+0.1;
double rate = rate1>rate2?rate1:rate2;
new_w=(int)(((double)img.getWidth(null))/rate);
new_h=(int)(((double)img.getHeight(null))/rate);
}
else{
new_w = width;//输出的图片宽度
new_h = height;//输出的图片高度
}
}
BufferedImage buffImg = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
Graphics g = buffImg.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, new_w, new_h);
g.drawImage(img,0,0,new_w,new_h,null);
g.dispose();
try {
OutputStream tempout = new FileOutputStream("C:\\temp.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
encoder.encode(buffImg);
tempout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tk.createImage("C:\\temp.jpg");
}
*/
private void jbInit() throws Exception {
// TODO Auto-generated method stub
component1 = Box.createGlue();
component1.addNotify();
device = CaptureDeviceManager.getDevice(str2);
locator = device.getLocator();
try{
player = Manager.createRealizedPlayer(locator);
player.start();
if((component1 = player.getVisualComponent())!=null){
this.getContentPane().add(component1,null);
}
}catch (Exception e){e.printStackTrace();}
jButton1.setBounds(new Rectangle(340,30,73,25));
jButton1.setText("拍照");
jButton1.addActionListener(new Camera_jButton1_actionAdapter(this));
this.getContentPane().setLayout(null);
jButton2.setText("保存");
jButton2.addActionListener(new Camera_jButton2_actionAdapter(this));
component1.setBounds(new Rectangle(10,10,320,240));
jLabel1.setIconTextGap(4);
jLabel1.setText("空");
jLabel1.setVerticalTextPosition(SwingConstants.CENTER);
jLabel1.setBounds(new Rectangle(10,250,320,240));
this.getContentPane().add(jLabel1,null);
this.getContentPane().add(jButton1,null);
this.getContentPane().add(jButton2,null);
jButton2.setBounds(new Rectangle(340,80,73,25));
this.setSize(450,600);
this.setVisible(true);
}
void jButton1_actionPerFormed(ActionEvent e){
iicon = new ImageIcon();
FrameGrabbingControl fgc = (FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
buffer = fgc.grabFrame();
b2i = new BufferToImage((VideoFormat)buffer.getFormat());
image = b2i.createImage(buffer);
iicon = new ImageIcon();
// iicon.setImage(this.resize(320, 240, image, true));
jLabel1.setIcon(iicon);
}
void jButton2_actionPerFormed(ActionEvent e){
JFileChooser filechooser = new JFileChooser();
filechooser.setFileHidingEnabled(true);
filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = filechooser.showSaveDialog(null);
if(result == JFileChooser.CANCEL_OPTION){
return ;
}
File file = filechooser.getSelectedFile();
if(file==null||file.getName().equals("")){
JOptionPane.showMessageDialog(null, "无效的文件名","警告",JOptionPane.ERROR_MESSAGE);
}
else
{
String s = file.getAbsolutePath();
BufferedImage bi = (BufferedImage)createImage(image.getWidth(null),image.getHeight(null));
Graphics2D g2 = bi.createGraphics();
g2.drawImage(image,null,null);
FileOutputStream out = null;
try {
out = new FileOutputStream(s);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(bi);
out.close();
} catch (ImageFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Camera camera1 = new Camera();
}
}
class Camera_jButton1_actionAdapter implements java.awt.event.ActionListener{
Camera adaptee;
Camera_jButton1_actionAdapter(Camera adaptee){
this.adaptee=adaptee;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
adaptee.jButton1_actionPerFormed(e);
}
}
class Camera_jButton2_actionAdapter implements java.awt.event.ActionListener{
Camera adaptee;
Camera_jButton2_actionAdapter(Camera adaptee){
this.adaptee=adaptee;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
adaptee.jButton2_actionPerFormed(e);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Camera.rar (6个子文件)
Camera
.project 382B
bin
Camera_jButton2_actionAdapter.class 617B
Camera_jButton1_actionAdapter.class 617B
Camera.class 7KB
src
Camera.java 7KB
.classpath 232B
共 6 条
- 1
资源评论
- 刹那552014-10-29只是用来调摄像头的,不是相机
- oudil682012-03-23为什么我用eclipse打开有错误啊?
- hyayh2013-12-10很一般了,不过还是感谢分享~~
- ainiyue13142013-10-14还在研究中!对自己现在的项目有帮助!
dongtinghong
- 粉丝: 99
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功