package com.gavin.gui;

import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.filechooser.FileSystemView;

import com.gavin.util.FTPUtil;
import com.gavin.util.PropertiesUtil;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;


public class PECamera {
	static boolean flag=false;  
	
	/**
	 *获取图标
	 *@param name img路径
	 *@return ImageIcon 
	 */
    private static ImageIcon produceImage(String name) {
		ImageIcon backImage = new ImageIcon(PECamera.class.getResource(name));
		return backImage;
	}
    
    /**
     * 从BufferedImage对象中获取InputStream对象  
     * @param img  BufferedImage
     * @param imgType
     * @return InputStream
     * @throws Exception
     */
  	private static InputStream getImageStream(BufferedImage img,String imgType) throws Exception{  
  	        InputStream in = null;   	          
  	        ByteArrayOutputStream bs = new ByteArrayOutputStream();   	          
  	        ImageOutputStream imOut;  
  	        try {  
  	            imOut = ImageIO.createImageOutputStream(bs);  
  	            ImageIO.write(img, imgType,imOut);                
  	            in= new ByteArrayInputStream(bs.toByteArray());                
  	        } catch (IOException e) { 
  	        	throw new Exception("图片流转换失败!",e);
  	        }   
  	        return in;  
  	    }  
  	
  	/**
  	 * 开启相机
  	 * @param fileName 图片保存名称
  	 * @throws Exception 
  	 */
  	public static void openCamera(final String fileName) throws Exception{
  		final String imgType=fileName.substring(fileName.lastIndexOf(".")+1);
  		 //open camera source  
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);  
        grabber.start();  
      
        final CanvasFrame canvasFrame = new CanvasFrame("PECamera");  
        IplImage image = grabber.grab();  
        int width = image.width();  
        int height = image.height();
        canvasFrame.setIconImage(produceImage("/img/timg.png").getImage());
        canvasFrame.setCanvasSize(width-10, height-10);  
   
        
        JToolBar jtb = new JToolBar();  
        final JButton jb1 = new JButton("拍照");  
        jb1.setToolTipText("点击拍照");
        final JButton jb2 = new JButton("重拍");  
        jb2.setToolTipText("重新拍照");
        final JButton jb3 = new JButton("保存");  
        jb3.setToolTipText("保存本地");
        final JButton jb4 = new JButton("上传");  
        jb4.setToolTipText("上传FTP");
        final JButton jb5 = new JButton("配置本地路径");  
        jb3.setToolTipText("保存本地");
        final JButton jb6 = new JButton("配置FTP路径"); 
        jb2.setEnabled(false);
        jb3.setEnabled(false);
        jb4.setEnabled(false);
        jtb.add(jb1);
        jtb.add(jb2);
        jtb.add(jb3);
        jtb.add(jb4);
        jtb.add(jb5);
        jtb.add(jb6);
        canvasFrame.add(jtb, BorderLayout.NORTH);

        

        
        
        //onscreen buffer for image capture  
        final BufferedImage bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        Graphics2D bGraphics = bImage.createGraphics();       
        
        //拍照按钮事件
        jb1.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				flag=true;
				jb1.setEnabled(false);
				jb2.setEnabled(true);
				jb3.setEnabled(true);
				jb4.setEnabled(true);
			}
		});
        //重拍按钮事件
        jb2.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				flag=false;
				jb1.setEnabled(true);
				jb2.setEnabled(false);
				jb3.setEnabled(false);
				jb3.setEnabled(false);
			}
		});  
        //保存按钮事件
        jb3.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				String localPath=PropertiesUtil.getPrimaryKey("localPath");
				if(localPath==null||localPath.isEmpty()){
					if(JOptionPane.showConfirmDialog(null,"尚未配置本地路径,现在配置?","",JOptionPane.YES_NO_OPTION)==0){
						jb5.doClick();
					}
				}else{
					try {  
	                    ImageIO.write(bImage, "jpg", new File(localPath+"\\"+fileName));  
	                    JOptionPane.showMessageDialog(canvasFrame, "保存成功", "成功", JOptionPane.INFORMATION_MESSAGE);
	                } catch (IOException e1) {  
	                	JOptionPane.showMessageDialog(canvasFrame, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
	                    e1.printStackTrace();  
	                }
				}
				
			}
		});
        //上传按钮事件
        jb4.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				String ftpPath=PropertiesUtil.getPrimaryKey("ftp_path");
				if(ftpPath==null||ftpPath.isEmpty()){
					if(JOptionPane.showConfirmDialog(null,"尚未配置FTP路径,现在配置?","",JOptionPane.YES_NO_OPTION)==0){
						jb6.doClick();
					}
				}else{
					try {  
						InputStream input=getImageStream(bImage, imgType);
	                    boolean ret=FTPUtil.uploadFile(ftpPath, fileName, input);
	                    if(ret)
	                    	JOptionPane.showMessageDialog(canvasFrame, "上传成功", "成功", JOptionPane.INFORMATION_MESSAGE);
	                    else{
	                    	JOptionPane.showMessageDialog(canvasFrame, "上传失败", "错误", JOptionPane.ERROR_MESSAGE);
	                    }
	                } catch (Exception e1) {  
	                	JOptionPane.showMessageDialog(canvasFrame, e1.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
	                    e1.printStackTrace();  
	                }
				}
				
			}
		});
        
        //配置本地路径按钮事件
        jb5.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				String rootPath=PropertiesUtil.getPrimaryKey("localPath");
				if(rootPath==null||rootPath.isEmpty()){
					FileSystemView fsv = FileSystemView.getFileSystemView();
					rootPath=fsv.getHomeDirectory().getPath();
				}				
				CustomFileChooser fileChooser = new CustomFileChooser(new File(rootPath));
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				fileChooser.setDialogTitle("本地存储路径");
				fileChooser.setApproveButtonText("确定");
				int returnVal = fileChooser.showOpenDialog(canvasFrame);
				if(returnVal == JFileChooser.APPROVE_OPTION){
				  String customPath= fileChooser.getSelectedFile().getAbsolutePath();
				  PropertiesUtil.saveKeyVal("localPath", customPath);
				}
			}
		});  
        //配置FTP路径按钮事件
        jb6.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				String ftpPath=PropertiesUtil.getPrimaryKey("ftp_path");
				if(ftpPath==null)ftpPath="";
				String inputValue = (String) JOptionPane.showInputDialog(canvasFrame, "", "请输入FTP地址", JOptionPane.INFORMATION_MESSAGE, null, null, ftpPath);
				if(inputValue!=null)
					PropertiesUtil.saveKeyVal("ftp_path", inputValue);
			}
		});  
        
        
        //图像区域鼠标点击事件  
        canvasFrame.getCanvas().addMouseListener(new MouseAdapter(){  
            public void mouseClicked(MouseEvent e){    
            	flag=flag?false:true;
               if(flag){
            	   jb1.doClick();
               }else{
            	   jb2.doClick();
               }
                                 
            }                  
        });  
           
        while(canvasFrame.isVisible() && (image=grabber.grab()) != null){  
            if(!flag) {
                canvasFrame.showImage(image); 
                //draw the onscreen image simutaneously  
                bGraphics.drawImage(image.getBufferedImage(),null,0,0);    
            }  
        }  
          
        //release resources  
        cvReleaseImage(image);     
        grabber.stop();  
        canvasFrame.dispose();  
  	}
  	
  	public static void main(String[] args){
  		try{
  			String fileName=args[0];
  			PECamera.openCamera(fileName);
  		}catch(Exception e){
  			e.printStackTrace();
  		}
  	}
  	
}
最近下载更多
sswert  LV2 2022年10月21日
crosa_Don  LV18 2022年6月7日
1265260263  LV4 2022年4月8日
zhou549014242  LV2 2021年10月31日
a82338181  LV7 2021年9月6日
15138754339  LV1 2021年8月16日
你是傻子  LV9 2021年5月24日
2235140624  LV17 2021年5月10日
lironggang  LV38 2020年12月25日
lzp12345687  LV10 2020年12月4日
最近浏览更多
22569889q 2023年6月30日
暂无贡献等级
毁灭者 2023年4月27日
暂无贡献等级
youwuzuichen  LV10 2023年3月22日
593174419  LV2 2023年1月9日
sswert  LV2 2022年10月21日
zhoujiangxi 2022年8月1日
暂无贡献等级
crosa_Don  LV18 2022年6月7日
xiao小果  LV12 2022年5月25日
1265260263  LV4 2022年4月8日
wang946427911 2022年4月2日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友