需求:使用虹软活体检测时,需要截取检测框中的人脸

Camera.PreviewCallback中onPreviewFrame(byte[] data, Camera camera)返回的data字节数组不是bitmap 的编码,需要转移一下:
下面列出几种方法:
1.有可能发生内存溢出:

       YuvImage image = new YuvImage(bytes, ImageFormat.NV21, width, height, null);ByteArrayOutputStream os = new ByteArrayOutputStream(bytes.length);if (!image.compressToJpeg(rect, 100, os)) {return null;}byte[] tmp = os.toByteArray();Bitmap bmp = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);

2.转码:

 //YCbCr_420_SP-->(解码,yuv420sp转为RGB格式 )static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width,int height) {final int frameSize = width * height;for (int j = 0, yp = 0; j < height; j++) {int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;for (int i = 0; i < width; i++, yp++) {int y = (0xff & ((int) yuv420sp[yp])) - 16;if (y < 0)y = 0;if ((i & 1) == 0) {v = (0xff & yuv420sp[uvp++]) - 128;u = (0xff & yuv420sp[uvp++]) - 128;}int y1192 = 1192 * y;int r = (y1192 + 1634 * v);int g = (y1192 - 833 * v - 400 * u);int b = (y1192 + 2066 * u);if (r < 0)r = 0;else if (r > 262143)r = 262143;if (g < 0)g = 0;else if (g > 262143)g = 262143;if (b < 0)b = 0;else if (b > 262143)b = 262143;rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000)| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);}}}

或者

 //图片字节转换public static Bitmap rawByteArray2RGBABitmap2(byte[] data, int width, int height) {int frameSize = width * height;int[] rgba = new int[frameSize];for (int i = 0; i < height; i++)for (int j = 0; j < width; j++) {int y = (0xff & ((int) data[i * width + j]));int u = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1)]));int v = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 1]));y = y < 16 ? 16 : y;int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128));int g = Math.round(1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));int b = Math.round(1.164f * (y - 16) + 2.018f * (u - 128));r = r < 0 ? 0 : (r > 255 ? 255 : r);g = g < 0 ? 0 : (g > 255 ? 255 : g);b = b < 0 ? 0 : (b > 255 ? 255 : b);rgba[i * width + j] = 0xff000000 + (b << 16) + (g << 8) + r;}Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);bmp.setPixels(rgba, 0, width, 0, 0, width, height);return bmp;}

3.这种需要sdk 17以上

    public Bitmap convertYUVtoRGB(byte[] yuvData, int width, int height) {if (yuvType == null) {yuvType = new Type.Builder(rs, Element.U8(rs)).setX(yuvData.length);in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);}in.copyFrom(yuvData);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {yuvToRgbIntrinsic.setInput(in);yuvToRgbIntrinsic.forEach(out);Bitmap bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);out.copyTo(bmpout);return bmpout;}return null;}
其中的对象初始化:RenderScript rs = RenderScript.create(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {ScriptIntrinsicYuvToRGB=  yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));}

现在说正题了:
前置左右镜像问题:

       final YuvImage image = new YuvImage(bytes, ImageFormat.NV21, width, height, null);ByteArrayOutputStream os = new ByteArrayOutputStream(bytes.length);if (!image.compressToJpeg(rect, 100, os)) {return null;}byte[] tmp = os.toByteArray();Bitmap bmp = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);Matrix matrix = new Matrix();Log.i(TAG, "getBitmapFromByte: displayOrientation-->" + displayOrientation);matrix.preRotate(360 - displayOrientation);//重点--->前置摄像头生成图片 左右镜像问题matrix.postScale(-1, 1);return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);

镜像上下颠倒,需要设置相机预览的显示方向

 public static void setCameraDisplayOrientation(Activity activity,int cameraId, android.hardware.Camera camera) {try {android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();android.hardware.Camera.getCameraInfo(cameraId, info);int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();int degrees = 0;switch (rotation) {case Surface.ROTATION_0:degrees = 0;break;case Surface.ROTATION_90:degrees = 90;break;case Surface.ROTATION_180:degrees = 180;break;case Surface.ROTATION_270:degrees = 270;break;}int result;if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {result = (info.orientation + degrees) % 360;result = (360 - result) % 360;  // compensate the mirror} else {  // back-facingresult = (info.orientation - degrees + 360) % 360;}camera.setDisplayOrientation(result);} catch (Exception e) {CommonUtil.printStackTrace(e);}}

之后做旋转:

      Matrix matrix = new Matrix();matrix.postRotate(CamParaUtil.getCameraDisplayOrientation(StickerMakeActivity.this, mCameraId, mCamera));matrix.postScale(scale, isCameraFront() ? -scale : scale);Bitmap cropRotateScaled = Bitmap.createBitmap(origin, (int) resultRect.left, (int) resultRect.top, (int) resultRect.width(), (int) resultRect.height(), matrix, true);

其中用到的方法:

public static int getCameraDisplayOrientation(Activity activity,int cameraId, android.hardware.Camera camera) {int result = 90;try {android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();android.hardware.Camera.getCameraInfo(cameraId, info);int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();int degrees = 0;switch (rotation) {case Surface.ROTATION_0:degrees = 0;break;case Surface.ROTATION_90:degrees = 90;break;case Surface.ROTATION_180:degrees = 180;break;case Surface.ROTATION_270:degrees = 270;break;}if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {result = (info.orientation + degrees) % 360;result = (360 - result) % 360;  // compensate the mirror} else {  // back-facingresult = (info.orientation - degrees + 360) % 360;}} catch (Exception e) {CommonUtil.printStackTrace(e);}return result;}

镜像来自自android开发步步为营之112:关于Camera镜像上下左右颠倒问题的解决办法

camera前置摄像头左右镜像问题相关推荐

  1. Android 前置摄像头预览与编码

    Android Camera前置摄像头采集.基于android.hardware.Camera,已经提示过时. 目标:在前置摄像头预览过程中,采集预览数据并编码到本地. 1. 设置摄像头的预览 获取摄 ...

  2. Think Pad 前置摄像头不能打开及其解决

    Think Pad 前置摄像头不能打开及其解决 LENOVO 20KS0028CD Windows 10 家庭版 Integrated Camera 前置摄像头无法打开,python调用摄像头失败,采 ...

  3. 前置摄像头 镜像_iPhone 前置摄像头拍照左右相反?试试 iOS 13 的照片编辑功能...

    喜欢使用 iPhone 拍照的小伙伴会发现,当使用前置摄像头自拍时,得到的照片左右似乎时相反的.这是因为,当你打开 iPhone 相机应用,调到前置摄像头开始拍照时,界面中显示的是镜像画面,就跟你平时 ...

  4. android 前置摄像头预览时 镜像翻转_全面屏时代,原来手机前置摄像头都隐藏着一些缺点,你发现了吗?...

    随着真全面屏时代的到来,人们已经不再满足于刘海屏.水滴屏以及挖孔屏等,越来越多的手机厂商和消费者开始追求"100%全面屏".于是如何解决手机前置摄像头便成了最大的难题,毕竟只要在屏 ...

  5. 【Android RTMP】NV21 图像旋转处理 ( 图像旋转算法 | 后置摄像头顺时针旋转 90 度 | 前置摄像头顺时针旋转 90 度 )

    文章目录 安卓直播推流专栏博客总结 一. 后置摄像头顺时针旋转 90 度 二. 前置摄像头顺时针旋转 90 度 三. NV21 格式图像旋转代码 安卓直播推流专栏博客总结 Android RTMP 直 ...

  6. android打开系统前置摄像头驱动,android 调用系统前置摄像头

    第一种方式是采用MediaStore,调用系统原生的相机. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.pu ...

  7. 安卓java摄像机的_在Android系统中调用系统前置摄像头

    从Android 2.3 Gingerbread开始,原生支持前置摄像头.下面我们看看如何在程序里来调用前置的摄像头. 第一种方式是采用MediaStore,调用系统原生的相机. Intent int ...

  8. H5移动端前置摄像头成像方向错误,横屏方向错误

    前言 最近在做一个小h5页面,通过手机摄像头获取视频图像通过video播放出来,发现两个问题.  1.前置摄像头成像时,左右颠倒(举右手,视频显示左手) 正常视频原图(为了展示凸出把视频图像换为了卡通 ...

  9. uniapp和vue实现打开手机前置摄像头和相机的方法

    最近在项目遇到了一个问题,用uniapp的框架做一个功能,就是打开平板自带的前置摄像头拍照,uniapp上的功能只有打开默认相机的功能,但是前置摄像头需要你自己去手动转换一下,客户体验感很不好,产品经 ...

最新文章

  1. ckeditor5加字数_CKEditor5基本使用
  2. android activity生命周期的一张经典图片
  3. oracle用户锁定及修改密码
  4. 告诉服务器端当前请求的意图
  5. 利用C#线程窗口调试多线程程序
  6. 充分条件反过来是必要条件吗_“充分必要条件”引发的现实思考
  7. C/C++ scanf 函数中%s 和%c 的简单差别
  8. org.springframework.data.redis.serializer.SerializationException: Cannot serialize;
  9. SSE指令集学习之旅(一)
  10. 什么是python 包_什么是python
  11. 基于华为云的一个典型的持续部署方案
  12. 中国五大顶级域名9月第一周新增3.2万 美国净减7.6万个
  13. VS2015 property manager
  14. 数据结构与算法:十大排序算法之冒泡排序
  15. linux 用户与工作组
  16. 导弹拦截(数据加强版)
  17. [PMP]专题资源管理错题
  18. 缉拿IP冲突之后的“真凶”
  19. 计算机注销操作,电脑注销快捷键
  20. STM32——CAN通信实验

热门文章

  1. 软件测试 Linux基础
  2. GVIM开发工具的使用方法
  3. 伯努利分布与二项分布Binomial Distribution
  4. 将本地项目上传至git仓库的步骤
  5. 9.9. Date/Time Functions and Operators
  6. C语言sizeof函数解析
  7. 人工神经网络中的多模态神经元
  8. Day 12 狂神说Java基础笔记(JVM01-14)
  9. LVDS转LVDS,转VGA,TTL,HDMI
  10. SQL 语句中的 (+)