javascript 设置相机宽度和高度 phonegap 相机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27104686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:56:32  来源:igfitidea点击:

Set camera width and height phonegap camera

javascriptandroidcordovaphonegap-plugins

提问by Matthijs

I am currently in the process of creating a mobile app that uses the Phonegap (Cordova) camera plugin. It correctly captures the image and displays it where I want to, but I can't seem to set the targetWidth and targetHeight options, as described.

我目前正在创建一个使用Phonegap (Cordova) 相机插件的移动应用程序。它正确捕获图像并将其显示在我想要的位置,但我似乎无法设置 targetWidth 和 targetHeight 选项,如上所述。

targetWidth: Width in pixels to scale image. Must be used with targetHeight. Aspect ratio remains constant. (Number)

targetHeight: Height in pixels to scale image. Must be used with targetWidth. Aspect ratio remains constant. (Number)

targetWidth:缩放图像的宽度(以像素为单位)。必须与 targetHeight 一起使用。纵横比保持不变。(数字)

targetHeight:缩放图像的高度(以像素为单位)。必须与 targetWidth 一起使用。纵横比保持不变。(数字)

As I understand, this will change the image-width and height on output. They, however, don't seem to be working.

据我了解,这将改变输出的图像宽度和高度。然而,它们似乎不起作用。

A suggestion that I found while researching for a solution, said to use the optional parameter allowEdit. In this I could get the user to select a pre-set squared image. This however, doesn't seem to work either.

我在研究解决方案时发现的一个建议,说是使用可选参数allowEdit。在这里,我可以让用户选择一个预设的平方图像。然而,这似乎也不起作用。

See my code below for reference.

请参阅下面的代码以供参考。

camera: function() {
    //Fire up the camera!
    navigator.camera.getPicture(onSuccess, onFail, {
        destinationType: Camera.DestinationType.DATA_URL,
        allowEdit: true,
        targetWidth: 512,
        targetHeight: 512
    });
},

Neither of the attemps succeeded in what I wanted; a fixed width and height for the image captured.

两次尝试都没有达到我想要的效果。捕获的图像的固定宽度和高度。

How can I set the image width and height on this image?

如何在此图像上设置图像宽度和高度?

回答by Mohammad Nurdin

Try this one my friend. remove allowEdit : true

试试这个我的朋友。消除allowEdit : true

camera: function() {
        navigator.camera.getPicture(onSuccess, onFail, {
            quality: 50,
            targetWidth: 512,
            targetHeight: 512,
            destinationType: navigator.camera.DestinationType. DATA_URL,
            saveToPhotoAlbum: true,
            correctOrientation: true
        });
    }

回答by Hyman He

How about changing your mind to resizing image after it has been captured?

在捕获图像后改变主意调整图像大小如何?

Useful article for resizing image with HTML5 Canvas

使用 HTML5 Canvas 调整图像大小的有用文章

回答by AAhad

I use following and it works well.

我使用以下方法,效果很好。

{
   quality: 25,
   targetWidth: 500,
   targetHeight: 500,
   destinationType: Camera.DestinationType.FILE_URI,
   sourceType: Camera.PictureSourceType.CAMERA,
   correctOrientation: true
}

Its also possible to modify the plugin native codeas per own needs. In case you are trying on android. here is the fix.

也可以根据自己的需要修改插件本机代码。如果您正在尝试使用 android。这是解决方法。

Inside executefunction , both parameters are set by default as zero that means a full size captured by device, otherwise if some values are passed by argsparameters then those are taken into account.

execute函数内部,两个参数默认设置为零,这意味着设备捕获的完整大小,否则如果某些值由args参数传递,则将这些值考虑在内。

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

            this.callbackContext = callbackContext;

     if (action.equals("takePicture")) {
                int srcType = CAMERA;
                int destType = FILE_URI;
                this.saveToPhotoAlbum = false;
                this.targetHeight = 0;
                this.targetWidth = 0;
                this.encodingType = JPEG;
                this.mediaType = PICTURE;
                this.mQuality = 80;

                this.mQuality = args.getInt(0);
                destType = args.getInt(1);
                srcType = args.getInt(2);
                this.targetWidth = args.getInt(3);
                this.targetHeight = args.getInt(4);
                this.encodingType = args.getInt(5);
                this.mediaType = args.getInt(6);
                this.allowEdit = args.getBoolean(7);
                this.correctOrientation = args.getBoolean(8);
                this.saveToPhotoAlbum = args.getBoolean(9);

see : https://github.com/apache/cordova-plugin-camera/blob/master/src/android/CameraLauncher.java#L115

见:https: //github.com/apache/cordova-plugin-camera/blob/master/src/android/CameraLauncher.java#L115

If possible you can set it as well in the native code and works fine.

如果可能,您也可以在本机代码中设置它并且工作正常。