如何使用Android上的离子照相机插件来调整图库中图像的大小?

时间:2018-10-21 10:00:37

标签: javascript android ionic-framework

我想调整从图库中选取的图像的大小。我设置了targetHeighttargetWidth,但它们似乎被忽略了...

从相机中拾取图像时,设置allowEdit使我可以裁剪图像,但当图片来自画廊时则不能。

我尝试了一些Crop插件,但是它们不符合我的需求,或者我无法在Ionic V1上实现它们。

您对从图库中选择200 px x 200 px方形图片的代码有什么看法吗?

// source_type is either 'camera' or 'gallery'

$scope.takePicture = function(source_type)
{
    navigator.camera.getPicture(
    function(uri){

	$ionicPopup.alert({
	         title: 'New avatar',
	         template: 'Is uploading...'
	});
	
	// Create a root reference
	  var storageRef = firebase.storage().ref();
    var newAvatar = "test.jpg";

    var contentType = contentType || '';
    var sliceSize = sliceSize || 512;

    let byteCharacters = atob(uri);
    let byteArrays = [];
           
    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) 
    {
        let slice = byteCharacters.slice(offset, offset + sliceSize);
           
        let byteNumbers = new Array(slice.length);
        for (let i = 0; i < slice.length; i++) 
        {
            byteNumbers[i] = slice.charCodeAt(i);
        }
           
        let byteArray = new Uint8Array(byteNumbers);
           
        byteArrays.push(byteArray);
    }
           
    var mountainImagesRef = storageRef.child("storage/pics/"+newAvatar);
           
    var blob = new Blob(byteArrays);
           
    mountainImagesRef.put(blob).then(function(snapshot) 
    {
        console.log("Uploaded a base64 string!");
        $scope.addNewAvatar(newAvatar);
    });

    },
    function(){
	   $ionicPopup.alert({
	         title: 'Erreur',
	         template: 'Error'
	         });
    },
    {

      quality: 70,
    	targetHeight: 200,
    	allowEdit: true,
    	targetWidth: 200,
      destinationType: 0,
		  sourceType: source_type === 'gallery' ? 0 : 1,
      mediaType: 0,
      saveToPhotoAlbum: false
     }
     );
};

0 个答案:

没有答案