protected Object doInBackground(Object[] params) {
Camera.Parameters parameters = mCamera.getParameters();
int imageWidth = parameters.getPreviewSize().width;
int imageHeight = parameters.getPreviewSize().height;
int RGBData[] = new int[imageWidth * imageHeight];
decodeYUV420SP(RGBData, mData, imageWidth, imageHeight);// 解码,yuv420sp转为RGB格式
Bitmap bm = Bitmap.createBitmap(RGBData, imageWidth, imageHeight, Bitmap.Config.ARGB_8888);//填到bitmap中
//图片旋转才能正常识别人脸
int cameraId = 1;
bm = rotateBitmap(bm, cameraId == 0 ? 90 : 270);
if (bm == null) {
Log.d(TAG, "无可用图片!!!");
return null;
}
JniClient jni = new JniClient();
int width = bm.getWidth();
int height = bm.getHeight();
//把bitmap中的ARGB_8888格式转为Mat矩阵中可用的BGRA格式数据bgra
// 计算位图所占用的内存字节数 getRowBytes()*getHeight()
int bytes = bm.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); // Create a new buffer
bm.copyPixelsToBuffer(buffer); // Move the byte data to the buffer
byte[] bs = buffer.array(); // Get the underlying array containing the
//计算图像通道数ch
int ch = bs.length / (width * height);
if (ch < 1) {
ch = 1;
}
mFaceByte = bs;
mWidth1 = width;
mHeight1 = height;
mCh1 = mFaceByte.length / (width * height);
//计算图像通道数ch
if (mCh1 < 1) {
mCh1 = 1;
}
//剪切的人脸图像
Bitmap tFaceBmp = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
long t = System.currentTimeMillis();
String tRetStr = jni.CMDetectFace(bs, width, height, ch, "/storage/emulated/0/Android/data/system/", 1, tFaceBmp);
t = System.currentTimeMillis() - t;
Log.e("监测人脸,获取人脸范围及5个点的坐标共花费时间", "" + t);
if (TextUtils.isEmpty(tRetStr) || tRetStr.equals("")) {
Log.d(TAG, "没有人脸");
} else {
//分解字符串,获取脸的位置
String[] tFaceStrs = tRetStr.split(";");
int face_num = tFaceStrs.length;
Bitmap bitmap2 = bm.copy(Bitmap.Config.ARGB_8888, true);
mCanvas = new Canvas(bitmap2);
Paint paint = new Paint();
int tStokeWid = 1 + (width + height) / 300;
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);//不填充
paint.setStrokeWidth(tStokeWid); //线的宽度
for (int i = 0; i < face_num; i++) {
String[] vals = tFaceStrs[i].split(",");
int x = Integer.valueOf(vals[0]);
int y = Integer.valueOf(vals[1]);
int w = Integer.valueOf(vals[2]);
int h = Integer.valueOf(vals[3]);
int left = x;
int top = y;
int right = x + w;
int bottom = y + w;
mCanvas.drawRect(left, top, right, bottom, paint);
if (vals.length < 5) {
continue;
}
//画特征点
for (int j = 0; j < 5; j++) {
int ti = 4 + j * 2;
int px = Integer.valueOf(vals[ti]);
int py = Integer.valueOf(vals[ti + 1]);
Log.i("loadimg", "j=" + j + ",px=" + px + ", py=" + py);
mCanvas.drawCircle(px, py, tStokeWid, paint);
}
}
}
return null;
}
PHP中文网2017-04-18 09:16:39
This is a sub-thread, which can be used to process data. The next step is to pass the processed data Object to the main thread and then display it instead of drawing it in the sub-thread. Using Handler.post(new Runnable) should meet your requirements.