Android中调用系统相机获取照片的尺寸问题

第一次写博客,怎么还感觉有点紧张呢大笑大笑大笑。好了,不废话了,下面进入正题。

最近在开发中遇到一个问题,我通过调用系统相机拍照,然后获取到我拍的照片,但每次取到了照片都是被压缩过的。达不到我的需求,后台通过看很多大神的文章,终于实现了我想要的效果。

第一步:调用相机,并设置保存目录

//先验证手机是否有sdcard
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED))
{
    try {
        File dir = new File(Environment.getExternalStorageDirectory() + "/" +"abc");
        if (!dir.exists()) dir.mkdirs();
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File f = new File(dir, "123");
        Uri u = Uri.fromFile(f);
        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
        startActivityForResult(intent, TAKE_PICTURE);

    } catch (ActivityNotFoundException e) {
        Toast.makeText(SendInfoActivity.this, "没有找到储存目录", Toast.LENGTH_LONG).show();
    }

} else {
    Toast.makeText(SendInfoActivity.this, "没有储存卡", Toast.LENGTH_LONG).show();
}

第二步:在onActivityResult做相应的处理

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            switch (requestCode) {

                case TAKE_PICTURE:
                
                        File f = new File(Environment.getExternalStorageDirectory()

                                + "/" + "abc" + "/" + "123");
                        try {
                            Uri u = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(),
                                    f.getAbsolutePath(), null, null));
                            Bitmap bm =  getBitmapFromUri(getApplicationContext(),u);


                        } catch (FileNotFoundException e) {
                            e.printStackTrace();

                        }
                    
                    break;
            }

        }
    }
public static Bitmap getBitmapFromUri(Context context,Uri uri) {
   try {
      // 读取uri所在的图片
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
      return bitmap;
   } catch (Exception e) {
      Log.e("[Android]", e.getMessage());
      Log.e("[Android]", "目录为:" + uri);
      e.printStackTrace();
      return null;
   }
}


这样你就可以得到原始的,没被压缩过的图片了


那么问题来了,如果你觉得得到图片尺寸太大,那你就可以用下面的方法来设置图片的大小


public static Bitmap setBitmapSize(Bitmap bm,int newWidth, int newHeight){
      int width = bm.getWidth();
      int height = bm.getHeight();
      // 设置想要的大小  
//    int newWidth = 800;
//    int newHeight = 1200;
      // 计算缩放比例  
      float scaleWidth = ((float) newWidth) / width;
      float scaleHeight = ((float) newHeight) / height;
      Matrix matrix = new Matrix();
      matrix.postScale(scaleWidth, scaleHeight);
      Bitmap mbitmap = Bitmap.createBitmap(bm,0,0,width,height,matrix,true);
      return mbitmap;
   }

有了这些后,你又想把它上传到服务器,但是又觉得太占内存,想把它压缩一下,那下面的方法你就用的上了


public static String bitmapToBase64(Bitmap bitmap,String tag) {
   String result = "";
   ByteArrayOutputStream bos = null;
   try {
      if (null != bitmap) {
         bos = new ByteArrayOutputStream();
         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);// bitmap放入字节数组流中
        
            int options = 100;
            while (bos.toByteArray().length / 1024 > 50) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
               options -= 10;// 每次都减少10
               bos.reset();// 重置baos即清空baos
               bitmap.compress(Bitmap.CompressFormat.JPEG, options, bos);// 这里压缩(100 - options%,把压缩后的数据存放到baos            }
       
         bos.flush();// bos流缓存在内存中的数据全部输出,清空缓存
         bos.close();

         byte[] bitmapByte = bos.toByteArray();
         result = Base64.encodeToString(bitmapByte, Base64.DEFAULT);
      }
   } catch (Exception e) {
      e.printStackTrace();
   } finally {
      if (null != null) {
         try {
            bos.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
   return result;
}

作为小白的我,想不到你更多的需求了,就写到这儿吧

该博客只是记录本人在开发过程中遇到的问题,有不对的地方希望各位指正,同时也希望能帮到你一点大笑大笑大笑




猜你喜欢

转载自blog.csdn.net/qq_31197303/article/details/54175758