Android摄像头使用问题记录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ddxxii/article/details/80894110
一. camera.getParameters()时候抛出异常“getParameters failed (empty parameters)”

查看了here

由于本应用多处使用摄像头,在不断切换中,可能存在释放和预览相争问题,在遇到这个异常的时候,捕获后对摄像头做释放操作,即可恢复正常。

二. 预览使用surfaceview ,底部有视频(surface)等,需要设置surfaceview到顶部覆盖底部
//解决和底部视频广告重叠的时候不能显示问题
        surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
        //surfaceView放置在顶层,即始终位于最上层
        //surfaceView.setZOrderOnTop(true);//位于最上层
        surfaceView.setZOrderMediaOverlay(true);//覆盖底部视频
三. 预览后通过setPreviewCallback获取到的data无法转bitmap

因为data属于YUMImage格式,所以不能直接转bitmap,先处理一下byte,使得可以转bitmap

private byte[] zpiByte(byte[] data, int mWidth, int mHeight) {
        YuvImage image = new YuvImage(data, ImageFormat.NV21, mWidth, mHeight, null);//这个参数不要乱动,乱动会转错误
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compressToJpeg(new Rect(0, 0, mWidth, mHeight), 100, stream);//先按原尺寸转成jpeg的流,后面再压缩一次。这个也不要乱动参数
        byte[] finalface = stream.toByteArray();//此时的尺寸并不小,但我尝试在上面代码直接转小会错位,考虑到要经过网络,所以后面会再转换一次,但转换速度非常快,基本上不会影响性能
        int rotateAngle = mFaceFragment.getRotation();
        Log.i(TAG, "rotateAngle" + rotateAngle);
        if (rotateAngle > 0) {//如果检测的时候需要旋转,那么拍下来的图片也需要旋转 shi
            Bitmap bitmap;
            bitmap = BitmapFactory.decodeByteArray(finalface, 0, finalface.length);
            Matrix m = new Matrix();
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            m.setRotate(rotateAngle); // 旋转angle度
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, true);// 从新生成图片
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            finalface = baos.toByteArray();
            LogUtil.d(TAG, "facelength:" + finalface.length);
//                                }
        }
        return finalface;
    }

这样子得到的byte数组就可以转bitmap了。

四. Camera.PictureCallback不执行

在出现这种情况的时候,通常会有如下打印:

dalvikvm-heap(18804): Grow heap (frag case) to 13.091MB for 6291472-byte allocation

执行了回收,猜测是camera对象被GC回收了,处理则是改camera为强引用就ok了。

猜你喜欢

转载自blog.csdn.net/ddxxii/article/details/80894110
今日推荐