网上东拼西凑起来的demo,有很多不解,求大神不吝指导
我将onPreviewFrame的data在异步中去处理,处理完成后,我怎么让处理后的数据显示到屏幕上,
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;
}
데이터를 처리하는 데 사용할 수 있는 하위 스레드입니다. 다음 단계는 처리된 데이터 개체를 메인 스레드에 전달한 다음 하위 스레드에 그리는 대신 표시하는 것입니다. Handler.post(new Runnable)를 사용하면 요구 사항을 충족해야 합니다.