package com.dayu.recharge.activity;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.graphics.Rect;
|
import android.hardware.Camera;
|
import android.os.Bundle;
|
import android.os.Handler;
|
import android.os.Message;
|
import android.util.Log;
|
import android.view.Display;
|
import android.view.SurfaceHolder;
|
import android.view.SurfaceView;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.view.ViewGroup;
|
import android.view.WindowManager;
|
import android.widget.Button;
|
import android.widget.ImageView;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
|
import com.dayu.recharge.R;
|
import com.dayu.recharge.view.ViewfinderView;
|
import com.yunmai.cc.idcard.controler.CameraManager;
|
import com.yunmai.cc.idcard.controler.OcrConstant;
|
import com.yunmai.cc.idcard.controler.OcrManager;
|
import com.yunmai.cc.idcard.vo.IdCardInfoNew;
|
|
import java.io.UnsupportedEncodingException;
|
|
|
/**
|
* 视频识别类
|
*
|
* @author fangcm 2013-03-18
|
*/
|
|
public class CameraActivity extends Activity implements SurfaceHolder.Callback {
|
|
private final String TAG = "cc_idcard";
|
|
private TextView tvTips;
|
private SurfaceView sv_preview;
|
private SurfaceHolder surfaceHolder;
|
private CameraManager cameraManager;
|
private boolean autoFoucs = true;
|
private ViewfinderView finderView;
|
private OcrManager ocrManager;
|
private Rect rect;
|
private boolean cameraError = false;
|
// private boolean over = false;
|
|
private Button btnFlash, btnCancel;
|
private ImageView imgTips;
|
private ImageView iv, ivHead;
|
private int typeFront = 0;// 0不区分 ,1正面,2背面
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
// TODO Auto-generated method stub
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.camera);
|
|
typeFront = this.getIntent().getIntExtra("typeFront", 0);
|
initView();
|
cameraManager = new CameraManager(getBaseContext(), mHandler);
|
mCameraOpenThread.start();
|
try {
|
mCameraOpenThread.join();
|
mCameraOpenThread = null;
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
cameraError = true;
|
}
|
if (cameraError) {
|
Toast.makeText(getBaseContext(), "照相机未启动!", Toast.LENGTH_SHORT).show();
|
finish();
|
return;
|
}
|
setParameters();
|
|
}
|
|
private Thread mCameraOpenThread = new Thread(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
cameraManager.openCamera();
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
cameraError = true;
|
}
|
}
|
});
|
|
private void setParameters() {
|
|
WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
|
Display display = manager.getDefaultDisplay();
|
int wWidth = display.getWidth();
|
int wHeight = display.getHeight();
|
|
cameraManager.setCameraFlashModel(Camera.Parameters.FLASH_MODE_OFF);
|
|
float ps = wWidth * 1.0f / wHeight;
|
cameraManager.setPreviewSize(ps);
|
|
int pWidth = cameraManager.getPreviewWidth();
|
int pHeight = cameraManager.getPreviewHeight();
|
if (pWidth == 0 || pHeight == 0) {
|
Toast.makeText(getBaseContext(), "照相机未启动!!", Toast.LENGTH_SHORT).show();
|
finish();
|
return;
|
}
|
Log.i(TAG, pWidth + "<--------W----Preview-----H------->" + pHeight);
|
Log.i(TAG, wWidth + "<--------W----WindowManager-----H------->" + wHeight);
|
|
int tempWidth = pWidth;
|
int tempHeidht = pHeight;
|
float x = 100.0f;
|
int tempW = pWidth;
|
int tempH = pHeight;
|
if (wWidth > pWidth && wHeight > pHeight) {
|
while (wWidth > tempW && wHeight > tempH) {
|
x++;
|
Log.d(TAG, "---xx----->" + x / 100.0);
|
tempW = (int) (pWidth * x / 100.0);
|
tempH = (int) (pHeight * x / 100.0);
|
if (wWidth > tempW && wHeight > tempH) {
|
tempWidth = tempW;
|
tempHeidht = tempH;
|
}
|
}
|
Log.d(TAG, "<------11--wWidth > pWidth && wHeight > pHeight------>");
|
} else {
|
while (tempWidth > wWidth || tempHeidht > wHeight) {
|
x--;
|
Log.d(TAG, "---xx----->" + x / 100.0);
|
tempWidth = (int) (pWidth * x / 100.0);
|
tempHeidht = (int) (pHeight * x / 100.0);
|
}
|
Log.d(TAG, "<-----22---tempWidth > wWidth || tempHeidht > wHeight------>");
|
}
|
|
Log.d(TAG, tempWidth + "<--------W----setParameters-----H------->" + tempHeidht);
|
// tempWidth = 854;
|
// tempHeidht = 480;
|
ViewGroup.LayoutParams lp = sv_preview.getLayoutParams();
|
lp.width = tempWidth;
|
lp.height = tempHeidht;
|
sv_preview.getHolder().setFixedSize(tempWidth, tempHeidht);
|
sv_preview.setLayoutParams(lp);
|
|
surfaceHolder = sv_preview.getHolder();
|
surfaceHolder.addCallback(CameraActivity.this);
|
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
|
float[] img = finderView.initFinder(tempWidth, tempHeidht, mHandler, typeFront);
|
|
imgTips.setY(img[0]);
|
imgTips.setX(img[1]);
|
|
}
|
|
private void initView() {
|
sv_preview = (SurfaceView) findViewById(R.id.camera_sv);
|
finderView = (ViewfinderView) findViewById(R.id.camera_finderView);
|
tvTips = (TextView) findViewById(R.id.tv_tips);
|
btnCancel = (Button) findViewById(R.id.bt_cancel);
|
btnFlash = (Button) findViewById(R.id.bt_flash);
|
imgTips = (ImageView) findViewById(R.id.iv_tips);
|
switch (typeFront) {
|
case 0:
|
imgTips.setVisibility(View.GONE);
|
break;
|
case 1:
|
imgTips.setBackgroundResource(R.mipmap.people_face);
|
break;
|
case 2:
|
imgTips.setBackgroundResource(R.mipmap.emblem);
|
break;
|
|
default:
|
break;
|
}
|
|
btnFlash.setOnClickListener(listener);
|
btnCancel.setOnClickListener(listener);
|
}
|
|
private boolean isFlashOn = false;
|
|
private OnClickListener listener = new OnClickListener() {
|
|
@Override
|
public void onClick(View v) {
|
// TODO Auto-generated method stub
|
switch (v.getId()) {
|
case R.id.bt_cancel:
|
setResult(998);
|
finish();
|
break;
|
case R.id.bt_flash:
|
if (isFlashOn) {
|
try {
|
if (cameraManager.closeFlashlight()) {
|
btnFlash.setBackgroundDrawable(getResources().getDrawable(R.mipmap.flash_on_s));
|
isFlashOn = false;
|
}
|
} catch (Exception e) {
|
// TODO: handle exception
|
}
|
|
} else {
|
try {
|
if (cameraManager.openFlashlight()) {
|
btnFlash.setBackgroundDrawable(getResources().getDrawable(R.mipmap.flash_off_s));
|
isFlashOn = true;
|
}
|
} catch (Exception e) {
|
// TODO: handle exception
|
}
|
|
}
|
break;
|
|
default:
|
break;
|
}
|
}
|
};
|
|
private Handler mHandler = new Handler() {
|
|
@Override
|
public void handleMessage(Message msg) {
|
// TODO Auto-generated method stub
|
super.handleMessage(msg);
|
switch (msg.what) {
|
case OcrConstant.TAKE_PREVIEW_DATA_OK:
|
Log.d("yxl", "TAKE_PREVIEW_DATA_OK");
|
if (ocrManager == null) {
|
ocrManager = new OcrManager(mHandler, CameraActivity.this);
|
try {
|
rect = cameraManager.getViewfinder(finderView.getFinder());
|
} catch (Exception e) {
|
// TODO: handle exception
|
return;
|
}
|
|
}
|
|
byte[] data_p = (byte[]) msg.obj;
|
if (data_p != null && data_p.length > 0) {
|
// if(over){
|
// return;
|
// }
|
ocrManager.recognIDCard(data_p, cameraManager.getPreviewWidth(), cameraManager.getPreviewHeight(),
|
rect, 0, typeFront);
|
mHandler.sendEmptyMessageDelayed(OcrConstant.START_AUTOFOCUS, 100);
|
} else {
|
finderView.setLineRect(0);
|
Toast.makeText(getBaseContext(), "相机出现问题,请重启手机!", Toast.LENGTH_SHORT).show();
|
mHandler.sendEmptyMessageDelayed(OcrConstant.START_AUTOFOCUS, 500);
|
}
|
break;
|
case OcrConstant.RECOGN_OK:
|
Log.d("yxl", "RECOGN_OK");
|
mHandler.removeMessages(OcrConstant.TAKE_PREVIEW_DATA_OK);
|
mHandler.removeMessages(OcrConstant.START_AUTOFOCUS);
|
// String imgPath = "/sdcard/aidtest.jpg";
|
// String headPath = "/sdcard/aidheadtest.jpg";
|
|
byte[] headPath = new byte[2048 * 1024 * 2];
|
byte[] imgPath = new byte[2048 * 1024 * 2];
|
int[] headRect = new int[8];
|
int[] imgRect = new int[8];
|
|
IdCardInfoNew idCardInfo = ocrManager.getResultToByte(imgPath, imgRect, headPath, headRect);
|
try {
|
String OCRResult = new String(idCardInfo.getCharInfo(), "gbk");
|
Intent intent = new Intent();
|
intent.putExtra("OCRResult", OCRResult);
|
setResult(RESULT_OK, intent);
|
} catch (UnsupportedEncodingException e) {
|
throw new RuntimeException(e);
|
}
|
// IdCardInfo idCardInfo = ocrManager.getResult(imgPath, headPath);
|
// AppDemo.setResult(idCardInfo);
|
// Intent data2 = new Intent();
|
// data2.putExtra("idcardinfo", idCardInfo);
|
// setResult(200, data2);
|
finish();
|
break;
|
case OcrConstant.REPEAT_AUTOFOCUS:
|
Log.d("yxl", "REPEAT_AUTOFOCUS");
|
|
cameraManager.autoFoucs();
|
mHandler.sendEmptyMessageDelayed(OcrConstant.REPEAT_AUTOFOCUS, 2000);
|
break;
|
case OcrConstant.RECOGN_EG_TIME_OUT:
|
Log.d("yxl", "RECOGN_EG_TIME_OUT");
|
Toast.makeText(getBaseContext(), "引擎过期,请尽快更新!", Toast.LENGTH_LONG).show();
|
finish();
|
break;
|
case OcrConstant.RECOGN_EG_LICENSE:
|
Log.d("yxl", "RECOGN_EG_LICENSE");
|
|
int ret = -1;
|
if (msg.obj != null) {
|
ret = (Integer) msg.obj;
|
}
|
Toast.makeText(getBaseContext(), "授权失败-->" + ret, Toast.LENGTH_LONG).show();
|
finish();
|
break;
|
case OcrConstant.RECOGN_EG_INIT_ERROR:
|
Log.d("yxl", "RECOGN_EG_INIT_ERROR");
|
|
Toast.makeText(getBaseContext(), "引擎初始化失败!", Toast.LENGTH_LONG).show();
|
finish();
|
break;
|
case OcrConstant.START_AUTOFOCUS:
|
Log.d("yxl", "START_AUTOFOCUS");
|
|
if (autoFoucs) {
|
cameraManager.autoFoucs();
|
autoFoucs = false;
|
mHandler.sendEmptyMessageDelayed(OcrConstant.START_AUTOFOCUS, 500);
|
mHandler.sendEmptyMessageDelayed(OcrConstant.REPEAT_AUTOFOCUS, 1500);
|
} else {
|
cameraManager.autoFocusAndPreviewCallback();
|
}
|
break;
|
case OcrConstant.RECOGN_LINE_IN_RECT:
|
Log.d("yxl", "RECOGN_LINE_IN_RECT");
|
|
int restult = (Integer) msg.obj;
|
finderView.setLineRect(restult);
|
break;
|
case OcrConstant.RECOGN_TIPS:
|
Log.d("yxl", "RECOGN_TIPS");
|
int tips = (Integer) msg.obj;
|
switch (tips) {
|
case 1000:
|
tvTips.setText("请将身份证置于此区域\r\n尝试对齐边缘");
|
break;
|
case 1001:
|
tvTips.setText("距离稍近,请尝试远点");
|
break;
|
case 1002:
|
tvTips.setText("距离稍远,请尝试靠近");
|
break;
|
case 1003:
|
// tvTips.setText("图像模糊,请调整距离");
|
break;
|
case 1004:
|
tvTips.setText("图像倾斜,请保持水平拍摄");
|
break;
|
case 1005:
|
tvTips.setText("请将身份证置于此区域\r\n尝试对齐边缘");
|
break;
|
case 1006:
|
tvTips.setText("请将身份证正面至于此区域");
|
break;
|
case 1007:
|
tvTips.setText("请将身份证反面至于此区域");
|
break;
|
|
default:
|
break;
|
}
|
|
break;
|
default:
|
Log.d("yxl", "default");
|
cameraManager.initDisplay();
|
mHandler.sendEmptyMessageDelayed(OcrConstant.START_AUTOFOCUS, 500);
|
Toast.makeText(getBaseContext(), "<>" + msg.what, Toast.LENGTH_SHORT).show();
|
break;
|
}
|
}
|
|
};
|
|
@Override
|
public void surfaceCreated(SurfaceHolder holder) {
|
// TODO Auto-generated method stub
|
Log.d(TAG, "surfaceCreated");
|
if (!cameraManager.cameraOpened()) {
|
cameraManager.openCamera();
|
setParameters();
|
}
|
}
|
|
@Override
|
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
// TODO Auto-generated method stub
|
if (holder.getSurface() == null) {
|
Log.d(TAG, "holder.getSurface() == null");
|
return;
|
}
|
Log.v(TAG, "surfaceChanged. w=" + width + ". h=" + height);
|
surfaceHolder = holder;
|
cameraManager.setPreviewDisplay(surfaceHolder);
|
cameraManager.initDisplay();
|
mHandler.sendEmptyMessageDelayed(OcrConstant.START_AUTOFOCUS, 500);
|
|
}
|
|
@Override
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
// TODO Auto-generated method stub
|
Log.d(TAG, "surfaceDestroyed");
|
try {
|
cameraManager.closeCamera();
|
} catch (Exception e) {
|
// TODO: handle exception
|
}
|
|
surfaceHolder = null;
|
}
|
|
private void finishAll() {
|
if (cameraManager != null) {
|
try {
|
cameraManager.closeCamera();
|
} catch (Exception e) {
|
// TODO: handle exception
|
}
|
|
}
|
|
}
|
|
@Override
|
protected void onDestroy() {
|
// TODO Auto-generated method stub
|
super.onDestroy();
|
mHandler.removeMessages(OcrConstant.TAKE_PREVIEW_DATA_OK);
|
mHandler.removeMessages(OcrConstant.START_AUTOFOCUS);
|
finishAll();
|
|
}
|
|
}
|