首页 文章

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

提问于
浏览
14

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

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

据我了解,这将改变输出的图像宽度和高度 . 然而,他们似乎没有工作 .

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

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

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

这些尝试都没有按照我的意愿取得成功;捕获图像的固定宽度和高度 .

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

3 回答

  • 1

    我使用以下,它运作良好 .

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

    它也可以根据自己的需要修改 plugin native code . 如果你正在尝试Android . 这是修复 .

    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);
    

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

    如果可能的话,你可以在本机代码中设置它并且工作正常 .

  • 3

    试试这个我的朋友 . 删除 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
            });
        }
    
  • 0

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

    Useful article for resizing image with HTML5 Canvas

相关问题