您的位置:首页 > 其它

关于拍照相册读取裁剪图片显示图片的比较好的博客

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 
谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: