关于拍照相册读取裁剪图片显示图片的比较好的博客
2016-10-31 10:47
567 查看
转载自:http://blog.csdn.net/harvic880925/article/details/43163175
拍照及裁剪终极方案
首先声明两个Uri,一个保存拍照的结果,一个保存裁剪的结果:[java] view
plain copy
private static final int RESULT_CAMERA_ONLY = 100;
private static final int RESULT_CAMERA_CROP_PATH_RESULT = 301;
private ImageView mImage;
private Uri imageUri;
private Uri imageCropUri;
然后在OnCreate()函数中初始化:
[java] view
plain copy
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = getSDCardPath();
File file = new File(path + "/temp.jpg");
imageUri = Uri.fromFile(file);
File cropFile = new File(getSDCardPath() + "/temp_crop.jpg");
imageCropUri = Uri.fromFile(cropFile);
mImage = (ImageView) findViewById(R.id.image_result);
Button btn_take_camera_only = (Button) findViewById(R.id.btn_camera_only);
btn_take_camera_only.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
takeCameraOnly();
}
});
}
点击按钮时仅调起拍照Intent,将结果存在imageUri中
[java] view
plain copy
private void takeCameraOnly() {
Intent intent = null;
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, RESULT_CAMERA_ONLY);
}
在接收到返回的消息后,调起裁剪Intent:
[java] view
plain copy
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK)
return;
switch (requestCode) {
case RESULT_CAMERA_ONLY: {
cropImg(imageUri);
}
break;
}
}
其中cropImg(Uri uri)是调起裁剪Intent,代码如下:
[java] view
plain copy
public void cropImg(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 700);
intent.putExtra("outputY", 700);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCropUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, RESULT_CAMERA_CROP_PATH_RESULT);
}
将传进去的uri做为源数据,即被裁剪的数据,将结果存储在imageCropUri中;
然后接收返回的结果:
[java] view
plain copy
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK)
return;
switch (requestCode) {
case RESULT_CAMERA_ONLY: {
cropImg(imageUri);
}
break;
case RESULT_CAMERA_CROP_PATH_RESULT: {
Bundle extras = data.getExtras();
if (extras != null) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageCropUri));
mImage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
break;
}
}
将存储裁剪结果的imageCropUri,转换为Bitmap,然后在ImageView中显示;
完整的代码如下:
[java] view
plain copy
public class MainActivity extends Activity {
private static final int RESULT_CAMERA_ONLY = 100;
private static final int RESULT_CAMERA_CROP_PATH_RESULT = 301;
private ImageView mImage;
private Uri imageUri;
private Uri imageCropUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = getSDCardPath();
File file = new File(path + "/temp.jpg");
imageUri = Uri.fromFile(file);
File cropFile = new File(getSDCardPath() + "/temp_crop.jpg");
imageCropUri = Uri.fromFile(cropFile);
mImage = (ImageView) findViewById(R.id.image_result);
Button btn_take_camera_only = (Button) findViewById(R.id.btn_camera_only);
btn_take_camera_only.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
takeCameraOnly();
}
});
}
private void takeCameraOnly() {
Intent intent = null;
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, RESULT_CAMERA_ONLY);
}
public void cropImg(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 700);
intent.putExtra("outputY", 700);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageCropUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, RESULT_CAMERA_CROP_PATH_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK)
return;
switch (requestCode) {
case RESULT_CAMERA_ONLY: {
cropImg(imageUri);
}
break;
case RESULT_CAMERA_CROP_PATH_RESULT: {
Bundle extras = data.getExtras();
if (extras != null) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageCropUri));
mImage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
break;
}
}
public static String getSDCardPath() {
String cmd = "cat /proc/mounts";
Runtime run = Runtime.getRuntime();// 返回与当前 Java 应用程序相关的运行时对象
try {
Process p = run.exec(cmd);// 启动另一个进程来执行命令
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;
while ((lineStr = inBr.readLine()) != null) {
// 获得命令执行后在控制台的输出信息
if (lineStr.contains("sdcard")
&& lineStr.contains(".android_secure")) {
String[] strArray = lineStr.split(" ");
if (strArray != null && strArray.length >= 5) {
String result = strArray[1].replace("/.android_secure",
"");
return result;
}
}
// 检查命令是否执行失败。
if (p.waitFor() != 0 && p.exitValue() == 1) {
// p.exitValue()==0表示正常结束,1:非正常结束
}
}
inBr.close();
in.close();
} catch (Exception e) {
return Environment.getExternalStorageDirectory().getPath();
}
return Environment.getExternalStorageDirectory().getPath();
}
}
到这里,这篇文章就讲完了,写的比较乱,原理应该是讲清楚了,有关从相册选择及裁剪的部分在下篇中讲解。
源码内容:
1、BlogCameraOnly:仅拍照功能
2、BlogCameraCropCrash:第三部分对应的源码,根本起不来裁剪Intent,造成Crash
3、BlogCameraCropFinally:拍照及裁剪的终极方案;
如果本文有帮到你,记得关注哦
源码下载地址:http://download.csdn.net/detail/harvic880925/8412983
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/43163175
谢谢。
相关文章推荐
- Android开发:仿微信和QQ空间发说说相册读取、拍照、图片裁剪和图片上传服务器等功能的实现
- 图片上传、显示网络图片、相册选取、拍照选取、图片裁剪
- Android 拍照/相册 获取图片裁剪显示
- 关于IOS某图片添加控件,图片从相册或拍照保存后,再次进入时点击放大图无法显示的问题
- Android开发:相册读取、拍照、图片裁剪和图片上传服务器等功能的实现
- 拍照/从相册读取图片后进行裁剪的方法
- 关于博客中的126相册图片不显示的问题
- 拍照、从相册选图并对图片进行裁剪
- 拍照、从相册选图并对图片进行裁剪
- IOS学习笔记39——拍照、从相册选图并对图片进行裁剪
- 关于相册图片,和拍照图片的处理
- 制作电子相册(sd卡读取bmp图片显示在tft彩屏上)读取fat32的步骤非常简单的方法
- 史上最强Android 开启照相或者是从本地相册选中一张图片以后先裁剪在保存并显示的讲解附源码
- 关于Android 选择相册图片裁剪的坑
- ImageView---拍照或从相册取图片并裁剪
- android 打开默认的系统相册,或拍照选取图片,并图片裁剪;
- 拍照、从相册选图并对图片进行裁剪
- 关于从相册取出图片后,图片不能按原来角度显示的问题解决方案
- IOS学习笔记39——拍照、从相册选图并对图片进行裁剪
- Android拍照、相册选取、裁剪图片