前置摄像头捕获的图像质量很差

时间:2019-12-24 10:47:37

标签: android android-camera surfaceview

后置摄像头的捕获图像很好,但是前置摄像头的捕获图像质量很差。 据我所知,setPreviewSize是造成此类问题的原因,但后置摄像头预览中没有问题。 请询问发布的代码是否不足。有解释的解决方案非常感谢。

SurfaceView创建:

 public void surfaceCreated(SurfaceHolder arg0) {
            try {
                if (flag == 0) {
                    camera = Camera.open(0);
                } else {
                    camera = Camera.open(1);
                }
            } catch (RuntimeException e) {
                e.printStackTrace();
                return;
            }

            try {
                Camera.Parameters param;
                param = camera.getParameters();
                List<Camera.Size> sizes = param.getSupportedPreviewSizes();
                //get diff to get perfact preview sizes
                DisplayMetrics displaymetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                int height = displaymetrics.heightPixels;
                int width = displaymetrics.widthPixels;
                long diff = (height * 1000 / width);
                long cdistance = Integer.MAX_VALUE;
                int idx = 0;
                for (int i = 0; i < sizes.size(); i++) {
                    long value = (long) (sizes.get(i).width * 1000) / sizes.get(i).height;
                    if (value > diff && value < cdistance) {
                        idx = i;
                        cdistance = value;
                    }
                }
                Camera.Size cs = sizes.get(0);
                param.setPreviewSize(cs.width, cs.height);
                param.setPictureSize(cs.width, cs.height);
                camera.setParameters(param);
                setCameraDisplayOrientation(0);

                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();

                if (flashType == 1) {
                    param.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
                    imgFlashOnOff.setImageResource(R.drawable.ic_flash_auto);

                } else if (flashType == 2) {
                    param.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
                    Camera.Parameters params = null;
                    if (camera != null) {
                        params = camera.getParameters();

                        if (params != null) {
                            List<String> supportedFlashModes = params.getSupportedFlashModes();

                            if (supportedFlashModes != null) {
                                if (supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
                                    param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                                } else if (supportedFlashModes.contains(Camera.Parameters.FLASH_MODE_ON)) {
                                    param.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
                                }
                            }
                        }
                    }
                    imgFlashOnOff.setImageResource(R.drawable.ic_flash_on);

                } else if (flashType == 3) {
                    param.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    imgFlashOnOff.setImageResource(R.drawable.ic_flash_off);
                }


            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }

0 个答案:

没有答案