Android 我修改了onPreviewFrame里返回的预览数据,怎么将修改后的数据显示到屏幕上

浏览:26日期:2022-11-07

问题描述

网上东拼西凑起来的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 bufferbm.copyPixelsToBuffer(buffer); // Move the byte data to the bufferbyte[] bs = buffer.array(); // Get the underlying array containing the//计算图像通道数chint ch = bs.length / (width * height);if (ch < 1) { ch = 1;}mFaceByte = bs;mWidth1 = width;mHeight1 = height;mCh1 = mFaceByte.length / (width * height);//计算图像通道数chif (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; }

问题解答

回答1:

这个是子线程,用来处理数据是可以的,下一步是把处理好的数据Object传给主线程,然后展示,而不是在子线程中绘制。使用Handler.post(new Runnable)应该可以满足你的要求。

相关文章: