• 安卓高级6 拍照或者从相册获取图片 并检测旋转角度或者更新画册扫描


    1. 旋转角度

    当我们从手机读取图片时候发现其图片旋转了90或者其他度数,比如三星拍照后的照片就是个例子.这时候我们读取出来判断角度然后在逆向回转为正向的图片

    1. 所需类 ExifInterface
      Exif是 Exchangeable Image File 缩写
    /**
         * 读取图片属性:旋转的角度
         * 
         * @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 rotateImage(int angle, Bitmap bitmap) {
            // 图片旋转矩阵
            Matrix matrix = new Matrix();
            matrix.postRotate(angle);
            // 得到旋转后的图片
            Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            return resizedBitmap;
        }

    2. 调用系统的剪切图片软件

    当我们某个图片过大 想让用户自行选择剪切区域

    1. 意图Intent的action “com.android.camera.action.CROP”
    2. 意图Intent的data类型 文件地址Uri 如Uri.fromFile(new File(fileSrc))
    3. 意图intent的type类型(mime) “image/*”

    以下是可以传入到intent的数值 (putExtra)

    数值名(字符串类型) 传入值类型 作用 案例
    crop String 设置true才能出剪辑的小方框,不然没有剪辑功能,只能选取图片 intent.putExtra(“crop”, “true”);
    aspectX int 放大缩小比例的X intent.putExtra(“aspectX”, 1);
    aspectY int 放大缩小比例的Y intent.putExtra(“aspectY”, 1);
    outputX int //这个是限制输出图片x方向大小(最大限制) intent.putExtra(“outputX”, 320);
    outputY int //这个是限制输出图片y方向大小(最大限制) intent.putExtra(“outputY”, 320);
    return-data boolean 是否返回数据图(后面有案例) intent.putExtra(“return-data”, true);
    scale和scaleUpIfNeeded boolean 切图大小不足输出,边缘出现类似毛边或者锯齿等 innerIntent.putExtra(“scale”, true);innerIntent.putExtra(“scaleUpIfNeeded”, true);
    /***
         * 裁剪图片
         * @param activity Activity
         * @param uri 图片的Uri
         */
        public static void cropPicture(Activity activity, Uri uri) {
            Intent innerIntent = new Intent("com.android.camera.action.CROP");
            innerIntent.setDataAndType(uri, "image/*");
            innerIntent.putExtra("crop", "true");// 设置true才能出剪辑的小方框,不然没有剪辑功能,只能选取图片
            innerIntent.putExtra("aspectX", 1); // 放大缩小比例的X
            innerIntent.putExtra("aspectY", 1);// 放大缩小比例的X   这里的比例为:   1:1
            innerIntent.putExtra("outputX", 320);  //这个是限制输出图片大小
            innerIntent.putExtra("outputY", 320); 
            innerIntent.putExtra("return-data", true);
            // 切图大小不足输出,无黑框
            innerIntent.putExtra("scale", true);
            innerIntent.putExtra("scaleUpIfNeeded", true);
            File imageFile = new File(getImagePath(activity.getApplicationContext()));
            innerIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
            innerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
            activity.startActivityForResult(innerIntent, REQUEST_CROP_IMAGE);
        }
    
    
    
    
    //回调:activityforresult
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data){
    // 获取返回数据
    Bitmap bmp = data.getParcelableExtra("data");
    
    }

    3. 拍照获取图片

    1. intent 的action: MediaStore.ACTION_IMAGE_CAPTURE
      直接案例:
    // 设置相机拍照后照片保存路径
                File mPictureFile = new File(Environment.getExternalStorageDirectory(), 
                        "picture" + System.currentTimeMillis()/1000 + ".jpg");
                // 启动拍照,并保存到临时文件
                Intent mIntent = new Intent();
                mIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                mIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPictureFile));
                mIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
    
                startActivityForResult(mIntent, REQUEST_CAMERA_IMAGE);

    回调acativityforesult

    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode != RESULT_OK) {
                return;
            }
    
    if (!mPictureFile.exists()) {
                    showTip("拍照失败,请重试");
                    return;
                }
    
                fileSrc = mPictureFile.getAbsolutePath();
    
    }
    

    4.更新画册

    当你更新一张图片在手机中时不会立即出现在图册中,需要重启手机或者通知系统去扫描

    private void updateGallery(String filename) {
            MediaScannerConnection.scanFile(this, new String[] {filename}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
    
                        @Override
                        public void onScanCompleted(String path, Uri uri) {
    
                        }
                    });
        }

    5.从手机相册获取

    直接案例

    Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_PICK);
                startActivityForResult(intent, REQUEST_PICTURE_CHOOSE);

    回调:

    
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode != RESULT_OK) {
                return;
            }
    if ("file".equals(data.getData().getScheme())) {
                    // 有些低版本机型返回的Uri模式为file
                    fileSrc = data.getData().getPath();
                } else {
                    // Uri模型为content
                    //选择数据地址字段
                    String[] proj = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(data.getData(), proj,
                            null, null, null);
                    cursor.moveToFirst();
                    //获取字段在第几列
                    int idx = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    //获取所在地址
                    fileSrc = cursor.getString(idx);
                    cursor.close();
                }
    
    }
  • 相关阅读:
    bean的作用域和生命周期
    装配Bean的三种方式
    BeanFactory和ApplicationContext对比
    创建 Spring容器的三种方式
    约束
    三大范式
    事务
    EdgeRank
    TCP三次握手四次挥手
    Linux权限解释
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152133.html
Copyright © 2020-2023  润新知