Android自定义相机(Cordova插件)的画质不佳

时间:2015-07-02 15:42:18

标签: android cordova camera

目前我正在使用自定义相机应用程序,预览看起来只是罚款。但是,当我拍摄照片并保存设备时,图片减少了80%(也是尺寸)。任何人都知道为什么会发生这种情况?同样在画廊,质量很差。我正在使用这个相机插件

https://github.com/performanceactive/phonegap-custom-camera-plugin/tree/master/platforms/android/src/com/performanceactive/plugins/camera

那就是参数。我无法添加所有代码(getPictureSave codes)。您可以从我分享的链接中看到。

... CameraPrewiev

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (getHolder().getSurface() == null) {
            return;
        }
        try {
            camera.stopPreview();
        } catch (Exception e){
            // tried to stop a non-existent preview
        }

        try {
            Camera.Parameters cameraParameters = camera.getParameters();
            Size previewSize = optimimalPreviewSize(width, height);
            cameraParameters.setPreviewSize(previewSize.width, previewSize.height);
            camera.setParameters(cameraParameters);
            camera.setPreviewDisplay(holder);
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (Exception e){
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }
 private Size optimimalPreviewSize(int targetWidth, int targetHeight) {
        List<Size> sizes = camera.getParameters().getSupportedPreviewSizes();
        float targetAspectRatio = (float) targetWidth / targetHeight;
        List<Size> sizesWithMatchingAspectRatios = filterByAspectRatio(targetAspectRatio, sizes);
        if (sizesWithMatchingAspectRatios.size() > 0) {
            return optimalSizeForHeight(sizesWithMatchingAspectRatios, targetHeight);
        }
        return optimalSizeForHeight(sizes, targetHeight);
    }

    private List<Size> filterByAspectRatio(float targetAspectRatio, List<Size> sizes) {
        List<Size> filteredSizes = new ArrayList<Size>();
        for (Size size : sizes) {
            // reverse the ratio calculation as we've flipped the orientation
            float aspectRatio = (float)size.height / size.width;
            if (aspectRatio == targetAspectRatio) {
                filteredSizes.add(size);
            }
        }
        return filteredSizes;
    }

    private Size optimalSizeForHeight(List<Size> sizes, int targetHeight) {
        Size optimalSize = null;
        float minimumHeightDelta = Float.MAX_VALUE;
        for (Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minimumHeightDelta) {
                optimalSize = size;
                minimumHeightDelta = Math.abs(size.height - targetHeight);
            }
        }
        return optimalSize;
    }

1 个答案:

答案 0 :(得分:2)

设置预览尺寸时,还应设置图片尺寸。最好将两者设置为具有相同的宽高比,否则一些设备会产生非常糟糕的静止图像。但是,您不必将它们设置为相等(例如,800x480的预览适用于图片大小2560 x 1440)。另请参阅 Camera in Android, how to get best size, preview size, picture size, view size, image distorted