[Phonegap+Sencha Touch] 移动开发30、phonegap的camera插件的几个问题

原文地址:http://blog.csdn.net/lovelyelfpop/article/details/38707501



navigator.camera.getPicture(function(uri){
    //得到拍摄的图片路径
}, function(err){}, {
    quality: 70,
    allowEdit: true, //拍摄完进行裁剪编辑
    targetWidth: 250,
    targetHeight: 250,
    destinationType: navigator.camera.DestinationType.FILE_URI,//返回uri
    sourceType: navigator.camera.PictureSourceType.CAMERA,//拍照
    saveToPhotoAlbum: true
});

phonegap的camera插件,在安卓系统上存在几个问题

1、得不到拍摄的照片的真实路径,具体查看博文《移动开发29 安卓navigator.camera.getPicture得到图片的真实路径》

2、targetWidth或者targetHeight大于512像素的时候,拍照并裁剪,app就会崩溃,或者app没反应(网上有人说有的是256像素,我的魅族MX3测试了是512像素)

3、只有在targetWidth == targetHeight的时候,裁剪图片才会限制比例1:1;否则,不能限制裁剪图长和宽的比例,可以调整任意。而我们有时候就是要限制裁剪后图片的长宽比。

4、如果设置了targetWidth和targetHeight为250*250,而实际裁剪的分辨率只有100*100,裁剪得到的图片会有黑边


解决办法:

1、第一个问题,解决办法查看博文《移动开发29 安卓navigator.camera.getPicture得到图片的真实路径》

2、第二个、第三个和第四个问题,改进如下:

进插件目录,找到此文件,org.apache.cordova.camera\src\android\CameraLauncher.java

307行附近getImage方法):
if (targetHeight > 0 && targetWidth > 0) { //限制裁减比例
  intent.putExtra("aspectX", targetWidth);
  intent.putExtra("aspectY", targetHeight);
}

361行附近performCrop方法
if (targetHeight > 0 && targetWidth > 0) { //限制裁减比例
    cropIntent.putExtra("aspectX", targetWidth);
    cropIntent.putExtra("aspectY", targetHeight);
}
// retrieve data on return
// cropIntent.putExtra("return-data", true);
cropIntent.putExtra("return-data", false); //存为文件而不返回Bitmap数据,解决裁减大图崩溃的现象
cropIntent.putExtra("scale", true); //去除黑边
cropIntent.putExtra("scaleUpIfNeeded", true); //去除黑边

File photo = createCaptureFile(encodingType);
croppedUri = Uri.fromFile(photo);
cropIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, croppedUri); //保存裁剪图为临时文件

674行附近onActivityResult方法):
/*注释掉下面这段代码
     * Bundle extras = intent.getExtras(); // get the cropped bitmap
     * Bitmap thePic = extras.getParcelable("data"); if (thePic ==
     * null) { this.failPicture("Crop returned no data."); return; }
     *
     * // now save the bitmap to a file OutputStream fOut = null;
     * File temp_file = new File(getTempDirectoryPath(),
     * System.currentTimeMillis() + ".jpg"); try {
     * temp_file.createNewFile(); fOut = new
     * FileOutputStream(temp_file);
     * thePic.compress(Bitmap.CompressFormat.JPEG, this.mQuality,
     * fOut); fOut.flush(); fOut.close(); } catch
     * (FileNotFoundException e) { e.printStackTrace(); } catch
     * (IOException e) { e.printStackTrace(); }
     */
    File photo = createCaptureFile(encodingType); 
    File temp_file = new File(getTempDirectoryPath(),
      System.currentTimeMillis() + ".jpg");
    if (photo.exists())
     photo.renameTo(temp_file); //得到裁剪图临时文件

    // // Send Uri back to JavaScript for viewing image
    this.callbackContext
      .success(Uri.fromFile(temp_file).toString());




欢迎加入Sencha Touch + Phonegap交流群

1群:194182999 (满)

2群:419834979

共同学习交流(博主QQ:479858761


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘_博士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值