package demo;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Combo;


import sun.misc.BASE64Decoder;


import CamVideo.VideoCtrl;


public class VideoCtrlWnd {
	
	private static VideoCtrl ocx;
	private static Shell sShell = null;
	private int camid=0;
	private Combo nameCombo;
	private Combo ResolutionCombo;
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			VideoCtrlWnd window = new VideoCtrlWnd();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.print("test");
	}
	
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		sShell.open();
		sShell.layout();
		while (!sShell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	
	public static boolean GenerateImage(String imgStr,String photoname)
    {
        //对字节数组字符串进行Base64解码并生成图片
        //图像数据为空
        if (imgStr == null)
            return false;


        BASE64Decoder decoder = new BASE64Decoder();
        try
        {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for(int i=0;i<b.length;++i)
            {
                if(b[i]<0)
                {
                    //调整异常数据
                    b[i]+=256;
                }
            }
            //生成jpeg图片
            //String imagePath= Config.getUploadPhysicalPath();
            //System.currentTimeMillis()
            //新生成的图片
            String imgFilePath = photoname;
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
	
	
	/**
	 * Create contents of the window.
	 */
	protected void createContents() {

		  sShell = new Shell();
		  sShell.setSize(850, 550);
		  sShell.setText("Video SWT DEMO");
		  sShell.setLayout(new GridLayout(2, false));
		  
		  ocx=new VideoCtrl();
		  ocx.InitOcx(sShell);		
		  ocx.SetFrameSize(600,500);	
		  ocx.SetClientSize(600,500);	
		  
		  
		  
		    
		  Group group = new Group(sShell, SWT.NONE);
          GridLayout layout = new GridLayout();
          layout.numColumns = 2;
          group.setLayout(layout);
            

		  nameCombo=new Combo(group, SWT.DROP_DOWN);
		  ResolutionCombo=new Combo(group, SWT.DROP_DOWN);
		  
		  int DeviceCount=ocx.GetDeviceCount();
		  for(int i=0;i<DeviceCount;i++)
		  {
			  nameCombo.add(ocx.GetDeviceName(i));
		  }
		  if(DeviceCount>0)
		  {
			  nameCombo.select(0);
			  camid=ocx.GetMainCameraID(1);
			  ocx.SetCamID(camid);
			  int ResolutionCount=ocx.GetResolutionCount();
			  for(int i=0;i<ResolutionCount;i++)
			  {
				  ResolutionCombo.add(ocx.GetResolutionStr(i));
			  }
			  if(ResolutionCount>0) ResolutionCombo.select(0);
		  }
		  
		  //切换设备响应事件
		  nameCombo.addSelectionListener(new SelectionAdapter(){
	            
	            public void widgetSelected(SelectionEvent e) {
	            	ocx.StopPreview();
	            	ResolutionCombo.removeAll();
	            	camid=nameCombo.getSelectionIndex();
		   			ocx.SetCamID(camid);
		   			int ResolutionCount=ocx.GetResolutionCount();
		   			for(int i=0;i<ResolutionCount;i++)
		   			{
		   				ResolutionCombo.add(ocx.GetResolutionStr(i));
		   			}
		   			if(ResolutionCount>0) ResolutionCombo.select(0);
	            }
	        });
		  
		//切换分辨率响应事件
		  ResolutionCombo.addSelectionListener(new SelectionAdapter(){
	            
	            public void widgetSelected(SelectionEvent e) {            	
	            	ocx.StopPreview();          	
	            	String value = ResolutionCombo.getItem(ResolutionCombo.getSelectionIndex());
	          		int pos= value.lastIndexOf("*");
	          		int width=Integer.parseInt(value.substring(0, pos));         		
	          		int heght=Integer.parseInt(value.substring(pos + 1));  
	          		int isOk=ocx.StartPreview(width,heght); //StartPreview返回值为0，表示设备开启成功，否则，失败
	          		System.out.print(isOk);
	            }
	        });


		  Button OpenBt = new Button(group, SWT.NONE);
		  OpenBt.setText(" 开 启 ");
		  Button CloseBt = new Button(group, SWT.NONE);
		  CloseBt.setText(" 关 闭 ");
		  Button CaptureImageBt = new Button(group, SWT.NONE);
		  CaptureImageBt.setText(" 拍 照 ");
		  Button setFilePathBt = new Button(group, SWT.NONE);
		  setFilePathBt.setText("设置路径");	  
		  Button ZoomOutBt = new Button(group, SWT.NONE);
		  ZoomOutBt.setText(" 放 大 ");
		  Button ZoomInBt = new Button(group, SWT.NONE);
		  ZoomInBt.setText(" 缩 小 ");
		  Button LeftBt = new Button(group, SWT.NONE);
		  LeftBt.setText(" 左 旋 ");
		  Button RightBt = new Button(group, SWT.NONE);
		  RightBt.setText(" 右 旋 ");
		  Button TrueSizeBt = new Button(group, SWT.NONE);
		  TrueSizeBt.setText("实际大小");
		  Button BestSizeBt = new Button(group, SWT.NONE);
		  BestSizeBt.setText("适合大小");
		  
		  Button StartIDCardBt = new Button(group, SWT.NONE);
		  StartIDCardBt.setText("初始化身份证功能");
		  Button GetOneICBt = new Button(group, SWT.NONE);
		  GetOneICBt.setText("读卡");
		  Button GetICValuesBt = new Button(group, SWT.NONE);
		  GetICValuesBt.setText("获取信息");
		  
		  
		  //开启按钮监听事件
		  OpenBt.addSelectionListener(new SelectionAdapter() {
          	public void widgetSelected(SelectionEvent evt){
          		String value = ResolutionCombo.getItem(ResolutionCombo.getSelectionIndex());
          		int pos= value.lastIndexOf("*");
          		int width=Integer.parseInt(value.substring(0, pos));         		
          		int heght=Integer.parseInt(value.substring(pos + 1));  
          		int isOk=ocx.StartPreview(width,heght); //StartPreview返回值为0，表示设备开启成功，否则，失败
          		System.out.print(isOk);
               }
            });
		  
		//关闭按钮监听事件
		  CloseBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		ocx.StopPreview();
	               }
	            });
		  
		//放大按钮监听事件
		  ZoomOutBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		ocx.ZoomOut();
	               }
	            });
		  
		//缩小按钮监听事件
		  ZoomInBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		ocx.ZoomIn();
	               }
	            });
		  
		//左旋按钮监听事件
		  LeftBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		ocx.LeftRoate();
	               }
	            });
		  
		//右旋监听事件
		  RightBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		ocx.RightRoate();
	               }
	            });
		  
		//实际大小按钮监听事件
		  TrueSizeBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		ocx.TrueSize();
	               }
	            });
		  
		//适合大小按钮监听事件
		  BestSizeBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		ocx.BestSize();
	               }
	            });
		  
		  StartIDCardBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		int ret = ocx.StartIDCard();
	          		if(ret==0){
	          			System.out.print("身份证初始化成功\r\n");    			
	          		}else System.out.print("身份证初始化失败\r\n");
	               }
	            });
		  
		  GetOneICBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		int ret = ocx.GetOneIC();
	          		if(ret==0){
	          			System.out.print("读卡成功\r\n");    			
	          		}else if(ret==-1 || ret==-2){
	          			System.out.print("没发现身份证\r\n");    			
	          		}else if(ret==-3){
	          			System.out.print("没发现身份证模块\r\n");    			
	          		}else if(ret==-4){
	          			System.out.print("未启动身份证模块功能\r\n");    			
	          		}
	               }
	            });
		  
		  GetICValuesBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		String str = ocx.GetICValues("NAME");
	          		System.out.print("NAME:"+str+"\r\n");
	          		
	          		str = ocx.GetICValues("IC");
	          		System.out.print("IC:"+str+"\r\n");
	          		
	          		str = ocx.GetICValues("SEX");
	          		System.out.print("SEX:"+str+"\r\n");
	          		
	          		str = ocx.GetICValues("NATION");
	          		System.out.print("NATION:"+str+"\r\n");
	          		
	          		str = ocx.GetICValues("BIRTHDAY");
	          		System.out.print("BIRTHDAY:"+str+"\r\n");
	          		
	          		str = ocx.GetICValues("ADDRESS");
	          		System.out.print("ADDRESS"+str+"\r\n");
	          		
	          		str = ocx.GetICValues("SIGNDEPT");
	          		System.out.print("SIGNDEPT"+str+"\r\n");
	          		
	          		str = ocx.GetICValues("VALIDSTARTDATE");
	          		System.out.print("VALIDSTARTDATE:"+str+"\r\n");
	          		
	          		str = ocx.GetICValues("VALIDENDDATE");
	          		System.out.print("VALIDENDDATE"+str+"\r\n");
	          		
	          		String base64Str = ocx.GetICPicture();
	          		//System.out.print("头像base64:   "+base64Str+"\r\n");
	          		
	               }
	            });
		  
		  CaptureImageBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		ocx.IsImageBase64(1);
	          		String str = ocx.CaptureImageJava();
	          		
	          		GenerateImage(str,"D:\\222.jpg");
	          		System.out.print("拍照返回: "+str+"\r\n");    	
	               }
	            });
		  
		  setFilePathBt.addSelectionListener(new SelectionAdapter() {
	          	public void widgetSelected(SelectionEvent evt){
	          		int ret = ocx.setFilePath("D:\\UploadFile");
	          		if(ret==0){
	          			System.out.print("设置路径成功\r\n");    			
	          		}else System.out.print("设置路径失败,路径不存在\r\n");
	               }
	            });
		  
		  
	 
	}
	


}
