简体   繁体   English

Android自定义相机(Cordova插件)的图片质量不佳

[英]Poor picture quality from Android Custom Camera (Cordova Plugin)

Currently i'm using a custom camera application, the preview looks just fines. 目前,我正在使用自定义相机应用程序,预览效果还不错。 But when i take the picture and save the devices the picture has decreased with like 80%(also size).Anyone knows why this is happening? 但是当我拍摄照片并保存设备时,照片却减少了80%(也就是尺寸)。有人知道为什么会这样吗? Also in gallery the quality is poor. 同样在画廊里,质量很差。 I am using this camera plugin 我正在使用这个相机插件

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

and thats the paramaters. 多数民众赞成在参数。 I can't add all codes( getPicture and Save codes ). 我不能添加所有代码( getPictureSave codes )。 You can see from the link I shared. 您可以从我共享的链接中看到。

CameraPrewiev... 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;
    }

When you set preview size, you should also set picture size. 设置预览尺寸时,还应该设置图片尺寸。 It is good practice to set both with same aspect ratio, otherwise some devices produce really bad stills. 最好将两者的长宽比设置为相同,否则某些设备会产生非常差的静止图像。 But you don't have to set them equal (eg preview of 800x480 works well with picture size 2560 x 1440). 但是您不必将它们设置为相等(例如,800x480的预览在2560 x 1440的图片尺寸下效果很好)。 See also Camera in Android, how to get best size, preview size, picture size, view size, image distorted . 另请参阅Android中的相机,如何获得最佳尺寸,预览尺寸,图片尺寸,视图尺寸,图像失真

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM