原文地址: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());