从相册选取图片并剪裁得一些方法



  /**
   * wy
   * 启动相机
   **/
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
//设置拍照后图片得保存地址

intent.putExtra(MediaStore.EXTRA_OUTPUT, pristineImageUri);
startActivityForResult(intent, TAKE_PICTURE_REQUEST_CODE);//or TAKE_SMALL_PICTURE
  /**
   * wy
   * 启动图库
   **/
   Intent  _intent = new Intent(Intent.ACTION_PICK);// ACTION_OPEN_DOCUMENT
   _intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
 
   //列表形式
   Intent _intent = new Intent(Intent.ACTION_GET_CONTENT, null);
   _intent.setType("image/*");

   startActivityForResult(_intent, albumImageRequestCode + tag);


/** * wy * 启动剪裁页 **/private void startPhotoZoom(Uri uri,int requestCode) {
    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", 4);
    intent.putExtra("aspectY", 3);

    // outputX,outputY 是剪裁图片的宽高
    intent.putExtra("outputX", 400);
    intent.putExtra("outputY", 300);

    //是否保留比例
    intent.putExtra("scale", true);

    //是否将数据保留在 Bitmap 中返回 dataParcelable 相应的 Bitmap 数据
    intent.putExtra("return-data", false);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, cropImageUri);
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

    startActivityForResult(intent, requestCode);


}

  /**
   * 获取剪裁后图片的 bitmap  在 onActivityResult 里执行对应的操作
   */
  private Bitmap decodeUriAsBitmap(Uri uri, Bitmap bitmap) {

      _width = storeImgArray[tag].getWidth();
      _height = storeImgArray[tag].getHeight();

      bitmap = decodeThumbBitmapForFile(uri.getPath(), _width, _height);

      return bitmap;

  }

  /**
   *  根据View(主要是ImageView)的宽和高来获取图片的缩略图
   *
   * @param path       SD卡路径
   * @param viewWidth  viewWidth
   * @param viewHeight viewHeight
   * @return Bitmap
   */
  private Bitmap decodeThumbBitmapForFile(String path, int viewWidth, int viewHeight) {
      BitmapFactory.Options options = new BitmapFactory.Options();
      //设置为true,表示解析Bitmap对象,该对象不占内存
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(path, options);
      //设置缩放比例
      options.inSampleSize = computeScale(options, viewWidth, viewHeight);

      //设置为false,解析Bitmap对象加入到内存中
      options.inJustDecodeBounds = false;

      return BitmapFactory.decodeFile(path, options);
  }


  /**
   * 根据View(主要是ImageView)的宽和高来计算Bitmap缩放比例。默认不缩放
   *
   * @param options    options
   * @param viewWidth  viewWidth
   * @param viewHeight viewHeight
   */
  private int computeScale(BitmapFactory.Options options, int viewWidth, int viewHeight) {
      int inSampleSize = 1;
      if (viewWidth == 0 || viewHeight == 0) {
          return inSampleSize;
      }
      int bitmapWidth = options.outWidth;
      int bitmapHeight = options.outHeight;

      //假如Bitmap的宽度或高度大于我们设定图片的View的宽高,则计算缩放比例
      if (bitmapWidth > viewWidth || bitmapHeight > viewWidth) {
          int widthScale = Math.round((float) bitmapWidth / (float) viewWidth);
          int heightScale = Math.round((float) bitmapHeight / (float) viewWidth);

          //为了保证图片不缩放变形,取宽高比例最小的那个
          inSampleSize = widthScale < heightScale ? widthScale : heightScale;
      }
      return inSampleSize;
    }


   /**
    * 获取 ImageView 里面的 Bitmap
    */
   public static Bitmap getImageViewBitmap(View view) {
       if (view == null) return null;
       if (view instanceof ImageView) {
           Drawable drawable = ((ImageView) view).getDrawable();
           if (drawable instanceof BitmapDrawable) { 
              return ((BitmapDrawable) drawable).getBitmap();
           }
       } 
       return null;
   }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值