package com.gary.fingerprint.proto;

import java.util.Arrays;

import com.gary.msr.hid.USBOp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
//import android.graphics.Bitmap.Config;
import android.hardware.usb.UsbManager;
import android.util.Log;

public class FingerprintPacket {
	//public static int[] PACKET = new int[9 * 1024];
	public static int[] Sendbuff = new int[30 * 1024*2];//2023新指纹*2
	public static byte[] imgbuff = new byte[30 * 1024*2];//2023新指纹*2
	public static int imgbuffLen=0;
	public static boolean isEndPack;
	public static final int HIDSUCC = 0;
	public static final int HIDREC_ERR = -1005;
	public static char CommandNum=0;  //命令标示号
    public static byte picturebuff[]=new byte[25700];   //保存图片数据
    public static char ReceiveDtaLen=0;                  //接受大包数据
    public static char picturelen=0;                  //图片已存长度
    public static boolean IsPicture=false;           //是否开始保存图片数据
    public static int fingerIMLen = 18432;			 //默认为新指纹模块图像大小 12800或者18432
	//    private static void clear() {
	//		Arrays.fill(Sendbuff, 0);
	//	}
	//    
    public USBOp mUSBOp;

	///20190822 添加构造函数
	public FingerprintPacket(UsbManager manager,Context context, String AppName)
	{
		
		mUSBOp= new USBOp(manager,context,AppName);
		if(!mUSBOp.UsbFeatureSupported()){
			Log.e("gary", "设备不支持USBHOST");
		}
		//mUSBOp.SetUsbConfig(0x0603, 0x096e, 0x03, 0x01, 0x02);
		mUSBOp.SetUsbConfig( 0x8235,0x10c4, 0x03, 0x01, 0x02);
		
	}
	public boolean OpenDeviceUsb()
	{
		
		return mUSBOp.USBOpInit();
	}
	public void CloseDeviceUsb()
	{
		mUSBOp.CloseDevice();
		
	}
    //返回数据处理
    //传入参数：ReadData--从USB读取到的64个字节  传出参数：ProcessResult--返回处理结果所带的参数（ProcessResult[0]-指令编号；ProcessResult[1-n]-参数；）
       public int USB_PS_ReadDataProcess(int[] ReadData,int[] ProcessResult)
       {
           //byte inbuff[]=new byte[25700];
           //char chkresult=0;
           int i=0;
           //len=0;
           if((ReadData[0]==0x08 && (ReadData[1]==0x20 || ReadData[1]==0xa0) && (ReadData[2]>0x00 || ReadData[3]>0x00)))
           {
               if((ReadData[4]==0xef && ReadData[5]==0x01  && ReadData[6]==0xff  && ReadData[7]==0xff  && ReadData[8]==0xff   && ReadData[9]==0xff))
               {
                       switch(CommandNum)
                       {
                            case 1:                        //自动注册
                               switch(ReadData[13])
                               {
                                   case 0x00:return 0;
                                   case 0x01:
                                       switch(ReadData[14])
                                       {
                                           case 0x01: return -1007;
                                           case 0x06: return -1011;
                                           default :return -1012;
                                       }
                                   case 0x07:return -1008;
                                   case 0x0a:return -1009;
                                   case 0x0b:return -1003;
                                   case 0x1f:return -1005;
                                   case 0x22:return -1006;
                                   case 0x25:return -1004;
                                   case 0x27:return -1010;
                                   default :return -1012;
                               }
                           case 2:                      //自动验证
                               switch(ReadData[13])
                               {
                                   case 0x00:
                                       ProcessResult[0]=2;
                                       ProcessResult[1]=ReadData[15];
                                       ProcessResult[2]=ReadData[16];
                                       ProcessResult[3]=ReadData[17];
                                       ProcessResult[4]=ReadData[18];
                                       return 0;

                                   case 0x01:
                                       switch(ReadData[14])
                                       {
                                           case 0x01: return -1007;
                                           default :return -1018;
                                       }
                               case 0x07:return -1008;
                               case 0x09:return -1013;
                               case 0x0b:return -1014;
                               case 0x17:return -1015;
                               case 0x23:return -1016;
                               case 0x24:return -1017;
                               case 0x27:return -1010;
                               default :return -1018;
                               }
                           case 3:                       //删除模板
                              if(ReadData[13]==0x00)
                                return 0;
                              else
                                return -1019;
                           case 4:                      //清空指纹库
                               if(ReadData[13]==0x00)
                                   return 0;
                                else if(ReadData[13]==1)
                                   return -1020;
                               else
                                   return -1021;
                           case 5:                      //取消指令
                               if(ReadData[13]==0x00)
                                   return 0;
                               else
                                   return -1022;
                           case 6:                    //读有效模板个数
                               if(ReadData[13]==0x00)
                               {
                                   ProcessResult[0]=6;
                                   ProcessResult[1]=ReadData[14];
                                   ProcessResult[2]=ReadData[15];
                                   return 0;
                               }
                               else
                                   return -1020;
                           case 7:                  //读索引表
                               if(ReadData[13]==0x00)
                               {
                                   ProcessResult[0]=7;
                                   i=0;
                                   for(i=1;i<33;i++)
                                   {
                                       ProcessResult[i]=ReadData[13+i];
                                   }
                                   return 0;
                               }
                               else
                                   return -1020;
                           case 8:                   //获取图像
                               if(ReadData[13]==0x00)
                                   return 0;
                               else
                                   return -1007;

                           case 9:                     //注册用获取图像
                               if(ReadData[13]==0x00)
                                   return 0;
                               else
                                   return -1007;
                           case 10:                //生成特征
                               switch(ReadData[13])
                               {
                                   case 0x00: return 0;
                                   case 0x01: return -1020;
                                   case 0x06: return -1023;
                                   case 0x07: return -1024;
                                   case 0x15: return -1025;
                                   default :return -1008;
                               }
                           case 11:                //合并模板
                               if(ReadData[13]==0x00)
                                   return 0;
                               else
                                   return -1009;
                           case 12:              //存储模板
                               if(ReadData[13]==0x00)
                                   return 0;
                               else
                                   return -1011;
                           case 13:             //搜索模板
                               if(ReadData[13]==0x00)
                               {
                                    ProcessResult[0]=13;
                                    ProcessResult[1]=ReadData[14];
                                    ProcessResult[2]=ReadData[15];
                                    ProcessResult[3]=ReadData[16];
                                    ProcessResult[4]=ReadData[17];
                                    return 0;
                               }
                               else if(ReadData[13]==0x01)
                                   return -1020;
                               else
                                   return -1013;
                           case 14:               //上传图像
                               if(ReadData[13]==0x00 && IsPicture==false)
                               {
                                   IsPicture=true;
                                   picturelen=0;
                                   ReceiveDtaLen=0;
                                   return -1027;
                               }
                               else if(ReadData[13]==0x01 && IsPicture==false)
                                   return -1020;
                               else if(ReadData[13]==0x0f && IsPicture==false)
                                   return -1026;
                               else if(IsPicture==true)
                               {
                                    i=0;
                                    if((ReadData[1]==0x20 && ReadData[10]==0x08))
                                    {
                                        for(i=13;i<271;i++)
                                        {
                                             picturebuff[picturelen]=(byte)ReadData[i];
                                             picturelen++;
                                        }
                                        IsPicture=false;
                                        picturelen=0;
                                        ReceiveDtaLen=0;
                                        return 0;
                                    }
                                    else if((ReadData[1]==0x20 && ReadData[10]==0x02))
                                    {
                                        for(i=13;i<271;i++)
                                        {
                                             picturebuff[picturelen]=(byte)ReadData[i];
                                             picturelen++;
                                        }
                                        return -1027;
                                    }
                               }
                               return -1002;
                           case 15:             //获取空白存储
                               if(ReadData[13]==0)
                               {
                                   ProcessResult[0]=15;
                                   ProcessResult[1]=ReadData[14];
                                   ProcessResult[2]=ReadData[15];
                                   return 0;
                               }
                               else if(ReadData[13]==0x1f)
                               {
                                   return -1005;
                               }
                               else
                                   return -1020;
                           case 16:              //设置口令
                               if(ReadData[13]==0x00)
                                   return 0;
                               else
                                   return -1020;
                           case 17:              //验证口令
                               if(ReadData[13]==0x00)
                                   return 0;
                               else if(ReadData[13]==0x13)
                                       return -1028;
                               else
                                   return -1020;
                        }
               }
               else
                 return -1002;
           }
           else
             return -1000;
           return -1000;
       }
    ////////////////////////////////////////////////////
    public static int CHK(byte[] Data,int Length)
    {
       int i;
       int backdata=0;
       for(i=10;i<(Length+10);i++)
       {
           backdata+=Data[i];
       }
       return backdata;
    }
    
    //获取图像
    //传入参数：无。 传出参数：Sendbuff--返回的数据
    /**
     * Functional description:Get image
     * @return  = 0  operation successfully <br> < 0  operation failed <br>
     * prerequisites USB device is successfully opened（OpenDeviceUsb）
     */
   public int USB_PS_GetImage()
   {
       byte inbuff[]=new byte[30];
       int []tmpesult=new int[64];
       int chkresult=0;
       byte i=0,len=0;
       int iRet;
       CommandNum=8;
       inbuff[0]=0x08;
       inbuff[1]=0x40;
       inbuff[2]=0x00;
       inbuff[3]=0x0c;
       inbuff[4]=(byte)0xef;
       inbuff[5]=0x01;
       inbuff[6]=(byte)0xff;
       inbuff[7]=(byte)0xff;
       inbuff[8]=(byte)0xff;
       inbuff[9]=(byte)0xff;
       inbuff[10]=0x01;
       inbuff[11]=0x00;
       inbuff[12]=0x03;
       inbuff[13]=0x01;
       chkresult=CHK(inbuff,4);
       inbuff[14]=(byte)(chkresult/256);
       inbuff[15]=(byte)(chkresult%256);
       len=16;
       for(i=0;i<len;i++)
       {
           Sendbuff[i]=inbuff[i];
       }
       iRet = mUSBOp.sendDatarFinger(Sendbuff, len);
       if (iRet < 0)
                        return HIDREC_ERR;
       iRet=USB_PS_ReadDataProcess(Sendbuff,tmpesult);
       if(iRet!=0)
           return iRet;
       return HIDSUCC;
   }
   
 //上传图像
   //传入参数：无。   传出参数：Sendbuff--返回的数据
   /**
    * Functional description: Upload image
    * @return  = 0  operation successfully <br> < 0  operation failed <br>
    * prerequisites USB device is successfully opened（OpenDeviceUsb）
    */
  public int USB_PS_UpImage() 
  {
      byte inbuff[]=new byte[30];
      int chkresult=0;
      int i=0,len=0;
      //int []tmpesult=new int[64];
      byte []tmpbufimg=new byte[30*1024*2];//20230302 新指纹仪修改
      int iRet;
      CommandNum=14;
      inbuff[0]=0x08;
      inbuff[1]=0x40;
      inbuff[2]=0x00;
      inbuff[3]=0x0c;
      inbuff[4]=(byte)0xef;
      inbuff[5]=0x01;
      inbuff[6]=(byte)0xff;
      inbuff[7]=(byte)0xff;
      inbuff[8]=(byte)0xff;
      inbuff[9]=(byte)0xff;
      inbuff[10]=0x01;
      inbuff[11]=0x00;
      inbuff[12]=0x03;
      inbuff[13]=0x0a;
      chkresult=CHK(inbuff,4);
      inbuff[14]=(byte)(chkresult/256);
      inbuff[15]=(byte)(chkresult%256);
      len=16;
      for(i=0;i<len;i++)
      {
          Sendbuff[i]=inbuff[i];
      }
      iRet = mUSBOp.sendDatarFingerImage(Sendbuff, len);
      if (iRet < 0)
          return HIDREC_ERR;
      imgbuffLen=iRet;
      Arrays.fill(imgbuff,(byte)0x00);
      for(i=0;i<imgbuffLen;i++)
    	  tmpbufimg[i]=(byte)(Sendbuff[i]&0xFF);
      //MakeBMPBuf(tmpbufimg,imgbuffLen,160,160,imgbuff);
      fingerIMLen=imgbuffLen;//20230203 为新指纹模块加入新旧指纹支持 以图像大小来判断
      if(imgbuffLen==(12800*2))
    	  MakeBMPBuf(tmpbufimg,imgbuffLen,160,160,imgbuff);
      else
    	  MakeBMPBuf(tmpbufimg,imgbuffLen,192,192,imgbuff);//2023新指纹 
      return HIDSUCC;
  }
  /**
   * Functional description: Return image
   * @return =Fingerprint picture BMP image
   * prerequisites USB_PS_GetImage is successful
   */
  public Bitmap USB_PS_GiveImage() //2023新指纹
  {
	  
	  //Bitmap nbitmap =BitmapFactory.decodeByteArray(imgbuff, 0, 160*160+1078);
	  if(imgbuffLen==12800*2)
	  {
		  Bitmap nbitmap =BitmapFactory.decodeByteArray(imgbuff, 0, 160*160+1078);
		  return nbitmap;
	  }
	  else
	  {
		  Bitmap nbitmap =BitmapFactory.decodeByteArray(imgbuff, 0, 192*192+1078);
		  return nbitmap;
	  }
	  //return nbitmap;
  
  }
//读索引表
  // 传入参数：page_number--页码。  传出参数：Sendbuff--返回的数据
  /**
   * Functional description:Read index table
   * @param page_number （0--Corresponding template index from 0-95）--input
   * @param outIdTable Readbuff：Read data returned（outIdTable[0--1]-Index information data length<br>
   * （=outIdTable[0]*256+outIdTable[1])outIdTable[2--n]--Index information<br>
   * (each 1 bit represents a template, =1 represents the mode corresponding to the storage<br> 
   * area Version has been entered, =0 means not entered））--output
   * @return  = 0  operation successfully <br> < 0  operation failed <br>
   * prerequisites USB device is successfully opened（OpenDeviceUsb）
   */
  public int USB_PS_ReadIndexTable(int page_number,int []outIdTable)
  {
      byte inbuff[]=new byte[30];
      int []tmpesult=new int[64];
      int chkresult=0;
      byte i=0,len=0;
      int iRet;
      CommandNum=7;
      inbuff[0]=0x08;
      inbuff[1]=0x40;
      inbuff[2]=0x00;
      inbuff[3]=0x0d;
      inbuff[4]=(byte)0xef;
      inbuff[5]=0x01;
      inbuff[6]=(byte)0xff;
      inbuff[7]=(byte)0xff;
      inbuff[8]=(byte)0xff;
      inbuff[9]=(byte)0xff;
      inbuff[10]=0x01;
      inbuff[11]=0x00;
      inbuff[12]=0x04;
      inbuff[13]=0x1f;
      inbuff[14]=(byte)(page_number%256);
      chkresult=CHK(inbuff,5);
      inbuff[15]=(byte)(chkresult/256);
      inbuff[16]=(byte)(chkresult%256);
      len=17;
      for(i=0;i<len;i++)
      {
          Sendbuff[i]=inbuff[i];
      }
      iRet = mUSBOp.sendDatarFinger(Sendbuff, len);
      if (iRet < 0)
          return HIDREC_ERR;
      iRet=USB_PS_ReadDataProcess(Sendbuff,tmpesult);
      if(iRet!=0)
          return iRet;
      System.arraycopy(tmpesult, 1, outIdTable, 0, 32);
      return HIDSUCC;
  }
  
  //生成特征
  //传入参数：BufferID--缓冲区号。  传出参数：Sendbuff--返回的数据
  /**
   * Functional description:Generating features
   * @param BufferID :Buffer number (1--4 during the registration process,<br>
   * BufferID indicates the first few fingers)--input
   * @return  = 0  operation successfully <br> < 0  operation failed <br>
   * prerequisites USB device is successfully opened（OpenDeviceUsb）
   */
  public int USB_PS_GetChar(int BufferID)
  {
      byte inbuff[]=new byte[30];
      int []tmpesult=new int[64];
      int chkresult=0;
      byte i=0,len=0;
      int iRet;
      CommandNum=10;
      inbuff[0]=0x08;
      inbuff[1]=0x40;
      inbuff[2]=0x00;
      inbuff[3]=0x0d;
      inbuff[4]=(byte)0xef;
      inbuff[5]=0x01;
      inbuff[6]=(byte)0xff;
      inbuff[7]=(byte)0xff;
      inbuff[8]=(byte)0xff;
      inbuff[9]=(byte)0xff;
      inbuff[10]=0x01;
      inbuff[11]=0x00;
      inbuff[12]=0x04;
      inbuff[13]=0x02;
      inbuff[14]=(byte)(BufferID%256);
      chkresult=CHK(inbuff,5);
      inbuff[15]=(byte)(chkresult/256);
      inbuff[16]=(byte)(chkresult%256);
      len=17;
      for(i=0;i<len;i++)
      {
          Sendbuff[i]=inbuff[i];
      }
      iRet = mUSBOp.sendDatarFinger(Sendbuff, len);
      if (iRet < 0)
               return HIDREC_ERR;
      iRet=USB_PS_ReadDataProcess(Sendbuff,tmpesult);
      if(iRet!=0)
          return iRet;
      return HIDSUCC;
  }
  //合并模板
   //传入参数：无。 传出参数：Sendbuff--返回的数据
  /**
   * Functional description:Merge template
   * @return  = 0  operation successfully <br> < 0  operation failed <br>
   * prerequisites USB device is successfully opened（OpenDeviceUsb）
   */
  public int USB_PS_RegMB()
  {
      byte inbuff[]=new byte[30];
      int []tmpesult=new int[64];
      int chkresult=0;
      byte i=0,len=0;
      int iRet;
      CommandNum=11;
      inbuff[0]=0x08;
      inbuff[1]=0x40;
      inbuff[2]=0x00;
      inbuff[3]=0x0c;
      inbuff[4]=(byte)0xef;
      inbuff[5]=0x01;
      inbuff[6]=(byte)0xff;
      inbuff[7]=(byte)0xff;
      inbuff[8]=(byte)0xff;
      inbuff[9]=(byte)0xff;
      inbuff[10]=0x01;
      inbuff[11]=0x00;
      inbuff[12]=0x03;
      inbuff[13]=0x05;
      chkresult=CHK(inbuff,4);
      inbuff[14]=(byte)(chkresult/256);
      inbuff[15]=(byte)(chkresult%256);
      len=16;
      for(i=0;i<len;i++)
      {
          Sendbuff[i]=inbuff[i];
      }
      iRet = mUSBOp.sendDatarFinger(Sendbuff, len);
      if (iRet < 0)
           return HIDREC_ERR;
      iRet=USB_PS_ReadDataProcess(Sendbuff,tmpesult);
      if(iRet!=0)
          return iRet;
      return HIDSUCC;
  }
  //存储模板
  //传入参数： BufferID--缓冲区号。 PageID--位置号。  传出参数：Sendbuff--返回的数据
  /**
   * Functional description:Storage template
   * @param BufferID: Buffer number（1--4，The default is 1）--input
   * @param PageID: User number（0--99）--input
   * @return  = 0  operation successfully <br> < 0  operation failed <br>
   * prerequisites USB device is successfully opened（OpenDeviceUsb）
   */
  public int USB_PS_StorMB(int BufferID,int PageID)
  {
      byte inbuff[]=new byte[30];
      int []tmpesult=new int[64];
      int chkresult=0;
      byte i=0,len=0;
      int iRet;
      CommandNum=12;
      inbuff[0]=0x08;
      inbuff[1]=0x40;
      inbuff[2]=0x00;
      inbuff[3]=0x0f;
      inbuff[4]=(byte)0xef;
      inbuff[5]=0x01;
      inbuff[6]=(byte)0xff;
      inbuff[7]=(byte)0xff;
      inbuff[8]=(byte)0xff;
      inbuff[9]=(byte)0xff;
      inbuff[10]=0x01;
      inbuff[11]=0x00;
      inbuff[12]=0x06;
      inbuff[13]=0x06;
      inbuff[14]=(byte)(BufferID%256);
      inbuff[15]=(byte)((PageID/256)%256);
      inbuff[16]=(byte)(PageID%256);
      chkresult=CHK(inbuff,7);
      inbuff[17]=(byte)(chkresult/256);
      inbuff[18]=(byte)(chkresult%256);
      len=19;
      for(i=0;i<len;i++)
      {
          Sendbuff[i]=inbuff[i];
      }
      iRet = mUSBOp.sendDatarFinger(Sendbuff, len);
      if (iRet < 0)
          return HIDREC_ERR;
      iRet=USB_PS_ReadDataProcess(Sendbuff,tmpesult);
      if(iRet!=0)
          return iRet;
      return HIDSUCC;
  }
  
//删除模板
  //传入参数：pageID--页码。 num--删除个数。传出参数：Sendbuff--返回的数据
  /**
   * Functional description:Delete template
   * @param pageID: page number（0--95）
   * @param num: Delete number(0--95)
   * @return  = 0  operation successfully <br> < 0  operation failed <br>
   * prerequisites USB device is successfully opened（OpenDeviceUsb）
   */
  public int USB_PS_DeletChar(int pageID,int num)
  {
      byte inbuff[]=new byte[30];
      int []tmpesult=new int[64];
      int chkresult=0;
      byte i=0,len=0;
      int iRet;
      CommandNum=3;
      inbuff[0]=0x08;
      inbuff[1]=0x40;
      inbuff[2]=0x00;
      inbuff[3]=0x10;
      inbuff[4]=(byte)0xef;
      inbuff[5]=0x01;
      inbuff[6]=(byte)0xff;
      inbuff[7]=(byte)0xff;
      inbuff[8]=(byte)0xff;
      inbuff[9]=(byte)0xff;
      inbuff[10]=0x01;
      inbuff[11]=0x00;
      inbuff[12]=0x07;
      inbuff[13]=0x0c;
      inbuff[14]=(byte)((pageID/256)%256);
      inbuff[15]=(byte)(pageID%256);
      inbuff[16]=(byte)((num/256)%256);
      inbuff[17]=(byte)(num%256);
      chkresult=CHK(inbuff,8);
      inbuff[18]=(byte)(chkresult/256);
      inbuff[19]=(byte)(chkresult%256);
      len=20;
      for(i=0;i<len;i++)
      {
          Sendbuff[i]=inbuff[i];
      }
      iRet = mUSBOp.sendDatarFinger(Sendbuff, len);
      if (iRet < 0)
               return HIDREC_ERR;
      iRet=USB_PS_ReadDataProcess(Sendbuff,tmpesult);
      if(iRet!=0)
          return iRet;
      return HIDSUCC;
  }
  //清空指纹库
  //传入参数：无。    传出参数：Sendbuff--返回的数据
  /**
   * Functional description:Clear fingerprint library
   * @return  = 0  operation successfully <br> < 0  operation failed <br>
   * prerequisites USB device is successfully opened（OpenDeviceUsb）
   */
  public int USB_PS_Empty()
  {
      byte inbuff[]=new byte[30];
      int []tmpesult=new int[64];
      int chkresult=0;
      byte i=0,len=0;
      int iRet;
      CommandNum=4;
      inbuff[0]=0x08;
      inbuff[1]=0x40;
      inbuff[2]=0x00;
      inbuff[3]=0x0c;
      inbuff[4]=(byte)0xef;
      inbuff[5]=0x01;
      inbuff[6]=(byte)0xff;
      inbuff[7]=(byte)0xff;
      inbuff[8]=(byte)0xff;
      inbuff[9]=(byte)0xff;
      inbuff[10]=0x01;
      inbuff[11]=0x00;
      inbuff[12]=0x03;
      inbuff[13]=0x0d;
      chkresult=CHK(inbuff,4);
      inbuff[14]=(byte)(chkresult/256);
      inbuff[15]=(byte)(chkresult%256);
      len=16;
      for(i=0;i<len;i++)
      {
          Sendbuff[i]=inbuff[i];
      }
      iRet = mUSBOp.sendDatarFinger(Sendbuff, len);
      if (iRet < 0)
           return HIDREC_ERR;
      iRet=USB_PS_ReadDataProcess(Sendbuff,tmpesult);
      if(iRet!=0)
          return iRet;
      return HIDSUCC;
  }
  
  //取消指令
  //传入参数：无。   传出参数：Sendbuff--返回的数据
  /**
   * Functional description:Cancel instruction
   * @return  = 0  operation successfully <br> < 0  operation failed <br>
   * prerequisites USB device is successfully opened（OpenDeviceUsb）
   */
  public int USB_PS_Cancel()
  {
      byte inbuff[]=new byte[30];
      int []tmpesult=new int[64];
      int chkresult=0;
      byte i=0,len=0;
      int iRet;
      CommandNum=5;
      inbuff[0]=0x08;
      inbuff[1]=0x40;
      inbuff[2]=0x00;
      inbuff[3]=0x0c;
      inbuff[4]=(byte)0xef;
      inbuff[5]=0x01;
      inbuff[6]=(byte)0xff;
      inbuff[7]=(byte)0xff;
      inbuff[8]=(byte)0xff;
      inbuff[9]=(byte)0xff;
      inbuff[10]=0x01;
      inbuff[11]=0x00;
      inbuff[12]=0x03;
      inbuff[13]=0x30;
      chkresult=CHK(inbuff,4);
      inbuff[14]=(byte)(chkresult/256);
      inbuff[15]=(byte)(chkresult%256);
      len=16;
      for(i=0;i<len;i++)
      {
          Sendbuff[i]=inbuff[i];
      }
      iRet = mUSBOp.sendDatarFinger(Sendbuff, len);
      if (iRet < 0)
          return HIDREC_ERR;
      iRet=USB_PS_ReadDataProcess(Sendbuff,tmpesult);
      if(iRet!=0)
          return iRet;
      return HIDSUCC;
  }
  
  //搜索模板
  //传入参数： BufferID--缓冲区号。 StartPage--起始页。PageNum--页数。  传出参数：Sendbuff--返回的数据
  /**
   * Functional description:Search template(1:n Comparison)
   * @param BufferID: Buffer number (1--4, default is 1)--input
   * @param StartPage: Start page（default is 0）--input
   * @param PageNum: Number of pages（default is 99）--input
   * @param recBuf: Read data returned（
   * recBuf[0--1]--page number(=recBuf[0]*256+recBuf[1])<br>
   * recBuf[2--3]--Score(=recBuf[2]*256+recBuf[3]))
   * @return  = 0  operation successfully <br> < 0  operation failed <br>
   * prerequisites USB device is successfully opened（OpenDeviceUsb）
   */
  public int USB_PS_SearchMB(int BufferID,int StartPage,int PageNum,int[]recBuf)
  {
      byte inbuff[]=new byte[30];
      int []tmpesult=new int[64];
      int chkresult=0;
      byte i=0,len=0;
      int iRet;
      CommandNum=13;
      inbuff[0]=0x08;
      inbuff[1]=0x40;
      inbuff[2]=0x00;
      inbuff[3]=0x11;
      inbuff[4]=(byte)0xef;
      inbuff[5]=0x01;
      inbuff[6]=(byte)0xff;
      inbuff[7]=(byte)0xff;
      inbuff[8]=(byte)0xff;
      inbuff[9]=(byte)0xff;
      inbuff[10]=0x01;
      inbuff[11]=0x00;
      inbuff[12]=0x08;
      inbuff[13]=0x04;
      inbuff[14]=(byte)(BufferID%256);
      inbuff[15]=(byte)((StartPage/256)%256);
      inbuff[16]=(byte)(StartPage%256);
      inbuff[17]=(byte)((PageNum/256)%256);
      inbuff[18]=(byte)(PageNum%256);
      chkresult=CHK(inbuff,9);
      inbuff[19]=(byte)(chkresult/256);
      inbuff[20]=(byte)(chkresult%256);
      len=21;
      for(i=0;i<len;i++)
      {
          Sendbuff[i]=inbuff[i];
      }
      iRet = mUSBOp.sendDatarFinger(Sendbuff, len);
      if (iRet < 0)
          return HIDREC_ERR;
      iRet=USB_PS_ReadDataProcess(Sendbuff,tmpesult);
      if(iRet!=0)
          return iRet;
      System.arraycopy(tmpesult, 1, recBuf, 0, 2);
      return HIDSUCC;
  }
  
  public static void MakeBMPBuf(byte[] inbuff,int inlen,int pictureWidth,int pictureHigh,byte[] picturebuff)
	{

	    //long datalen=0;
	    int i=0;
	    int j=0;
	    //BMP文件头（14字节）
	    picturebuff[0]=0x42;  //B
	    picturebuff[1]=0x4D;  //M
	    //datalen=1078+inlen;
	    //*picturelen=datalen;
	    //picturebuff[2]=0x36;  // 位图文件的大小，以字节为单位（3-6字节，低位在前）
	    //picturebuff[3]=0x68;
	    picturebuff[2]=0x00;
	    picturebuff[3]=0x00;
	    picturebuff[4]=0x00;
	    picturebuff[5]=0x00;
	    picturebuff[6]=0x00;  //  位图文件保留字，必须为0(7-8字节）
	    picturebuff[7]=0x00;
	    picturebuff[8]=0x00;  //  位图文件保留字，必须为0(9-10字节）
	    picturebuff[9]=0x00;
	    picturebuff[10]=0x36;  //  位图数据的起始位置，以相对于位图（11-14字节，低位在前）
	    picturebuff[11]=0x04;
	    picturebuff[12]=0x00;
	    picturebuff[13]=0x00;
	    //位图信息头（40字节）
	    picturebuff[14]=0x28;  // 本结构所占用字节数（15-18字节）
	    picturebuff[15]=0x00;
	    picturebuff[16]=0x00;
	    picturebuff[17]=0x00;
	    picturebuff[18]=(byte)pictureWidth;  // 位图的宽度，以像素为单位（19-22字节）
	    picturebuff[19]=0x00;
	    picturebuff[20]=0x00;
	    picturebuff[21]=0x00;
	    picturebuff[22]=(byte)pictureHigh;  // 位图的高度，以像素为单位（23-26字节）
	    picturebuff[23]=0x00;
	    picturebuff[24]=0x00;
	    picturebuff[25]=0x00;
	    picturebuff[26]=0x01;   //目标设备的级别，必须为1(27-28字节）
	    picturebuff[27]=0x00;
	    picturebuff[28]=0x08;   //每个像素所需的位数，（29-30字节）
	    picturebuff[29]=0x00;
	    picturebuff[30]=0x00;  // 位图压缩类型，必须是0（不压缩），（31-34字节）
	    picturebuff[31]=0x00;
	    picturebuff[32]=0x00;
	    picturebuff[33]=0x00;
	    picturebuff[34]=0x00;  //位图的大小(其中包含了为了补齐行数是4的倍数而添加的空字节)，以字节为单位（35-38字节）
	    picturebuff[35]=0x00;
	    picturebuff[36]=0x00;
	    picturebuff[37]=0x00;
	    picturebuff[38]=0x00;  // 位图水平分辨率，每米像素数（39-42字节）
	    picturebuff[39]=0x00;
	    picturebuff[40]=0x00;
	    picturebuff[41]=0x00;
	    picturebuff[42]=0x00;  // 位图垂直分辨率，每米像素数（43-46字节)
	    picturebuff[43]=0x00;
	    picturebuff[44]=0x00;
	    picturebuff[45]=0x00;
	    picturebuff[46]=0x00;  // 位图实际使用的颜色表中的颜色数（47-50字节）
	    picturebuff[47]=0x01;
	    picturebuff[48]=0x00;
	    picturebuff[49]=0x00;
	    picturebuff[50]=0x00;  // /位图显示过程中重要的颜色数（51-54字节）
	    picturebuff[51]=0x01;
	    picturebuff[52]=0x00;
	    picturebuff[53]=0x00;
	    //颜色表
	    for(i=0x00;i<256;i++)
	    {
	        picturebuff[54+i*4]=(byte)i;
	        picturebuff[55+i*4]=(byte)i;
	        picturebuff[56+i*4]=(byte)i;
	        picturebuff[57+i*4]=0x00;
	    }
	    
	    for(i=0;i<pictureHigh;i++)
	    {
	       for(j=0;j<pictureWidth;j++)
	       {
	            picturebuff[1078+((pictureHigh-1-i)*(pictureWidth))+j]=inbuff[i*pictureWidth+j];

	       }

	    }
	}
  
  
  
}