【问题标题】:Poor picture quality from Android Custom Camera (Cordova Plugin)Android 自定义相机(Cordova 插件)的图片质量很差
【发布时间】:2015-07-02 15:42:18
【问题描述】:

目前我正在使用自定义相机应用程序,预览看起来还不错。但是当我拍照并保存设备时,图片减少了 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.你提供了一个 ios 库的链接 2. 也许在 github 上打开一张票?
  • 抱歉,我立即更正了。

标签: android cordova camera


【解决方案1】:

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

【讨论】:

  • 感谢您的回答。答案很有帮助。它还有一个问题。如何获得尝试保存图像的方向值。但我不需要使用 eventListener 。因为那一刻我只想要那个位置。
  • orientation - 你是指纵向还是横向?您的 Activity 是否定义为横向锁定,还是跟随传感器?
  • 谢谢,我是说肖像。我锁定到 Portrait of UI 。我解决了这个问题。轮换的过程,我移到了预览的activity。
猜你喜欢
  • 2017-02-06
  • 2014-02-16
  • 1970-01-01
  • 2012-11-28
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
相关资源
最近更新 更多