欢迎使用CSDN-markdown编辑器

来源:互联网 发布:常用数据采集卡型号 编辑:程序博客网 时间:2024/05/21 07:52

android 原生访问系统相册 相机 裁剪

github demo

  • 系统相册
  • 系统相机
  • 裁剪
  • 存储

系统相机入口

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                startActivityForResult(camera, CAMERA);

onActivityResult处理

处理放弃操作

 if (resultCode != Activity.RESULT_OK || data == null) {            Toast.makeText(getActivity(), "you have cancel option", Toast.LENGTH_SHORT).show();            return;        }

获取到返回的Uri:

    data.getData() 

获取到返回的bitmap:

 Bitmap bitmap = data.getParcelableExtra("data") 

系统相册入口

intent.setAction(Intent.ACTION_GET_CONTENT);                intent.setType("image/*");                startActivityForResult(intent, GALLERY);

onActivityResult 处理与上面类似

裁剪

    /**     * 调用系统裁剪功能:     * 传入图片uri     * @param uri picture uri     */    private void startPhotoZoom(Uri uri) {        Log.i(TAG, " url================" + uri);        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(uri, "image/*");        // crop为true是设置在开启的intent中设置显示的view可以剪裁        intent.putExtra("crop", "true");        // aspectX aspectY 是宽高的比例        intent.putExtra("aspectX", 1);        intent.putExtra("aspectY", 1);        // outputX,outputY 是剪裁图片的宽高        intent.putExtra("outputX", 600);        intent.putExtra("outputY", 600);        intent.putExtra("return-data", true);        intent.putExtra("noFaceDetection", true);        startActivityForResult(intent, PHOTO_REQUEST_CUT);    }

onActivityResult 处理结果类似,这里获取返回的bitmap,将其显示在UI上,并且可以做持久化保存,保存的时候记得读写权限;
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

保存bitmap

private final String IMAGE_FILE_LOCATION =Environment.getExternalStorageDirectory() + "/temp.jpg";     /**     * 把头像保存在本地     * @param bm     * @return     */    private Uri saveBitmap(Bitmap bm) {        File tmpDir = new File(IMAGE_FILE_LOCATION);        tmpDir.delete();        if (!tmpDir.getParentFile().exists()) {            tmpDir.mkdir();        }        File img = new File(IMAGE_FILE_LOCATION);        try {            FileOutputStream fos = new FileOutputStream(img);            bm.compress(Bitmap.CompressFormat.PNG, 85, fos);            fos.flush();            fos.close();            Toast.makeText(getActivity(), "成功了", Toast.LENGTH_SHORT).show();            return Uri.fromFile(img);        } catch (FileNotFoundException e) {            Toast.makeText(getActivity(), "失敗了", Toast.LENGTH_SHORT).show();            e.printStackTrace();            return null;        } catch (IOException e) {            e.printStackTrace();            Toast.makeText(getActivity(), "失敗了", Toast.LENGTH_SHORT).show();            return null;        }    }

关于bitmap方向,没怎么搞懂,从别个哪里借鉴

    /**     * 1、bitmap  to  uri     * <p/>     * Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));     * <p/>     * 2、uri  to  bitmap     * <p/>     * Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);     * 读取图片属性:旋转的角度     *     * @param path 图片绝对路径     * @return degree旋转的角度     */    public static int readPictureDegree(String path) {        int degree = 0;        try {            ExifInterface exifInterface = new ExifInterface(path);            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);            switch (orientation) {                case ExifInterface.ORIENTATION_ROTATE_90:                    degree = 90;                    break;                case ExifInterface.ORIENTATION_ROTATE_180:                    degree = 180;                    break;                case ExifInterface.ORIENTATION_ROTATE_270:                    degree = 270;                    break;            }        } catch (IOException e) {            e.printStackTrace();        }        return degree;    }    /**     * 旋转图片     *     * @param angle     * @param bitmap     * @return Bitmap     */    public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {        //旋转图片 动作        Matrix matrix = new Matrix();        matrix.postRotate(angle);        System.out.println("angle2=" + angle);        // 创建新的图片        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,                bitmap.getWidth(), bitmap.getHeight(), matrix, true);        return resizedBitmap;    }

最后第三方开源

https://github.com/crazycodeboy/TakePhoto

0 0
原创粉丝点击