简体   繁体   English

在不改变分辨率的情况下减小图像尺寸 android studio

[英]Reduce image size without changing resolution android studio

In my android app, I use the Camera intent to click a photo and upload the same to my server for processing.在我的 android 应用程序中,我使用相机意图单击照片并将其上传到我的服务器进行处理。 However, there is a requirement that the resolution of the image should be more than 500X500.但是,要求图像的分辨率应大于 500X500。

The default camera resolution is much higher these days and hence the desired resolution is not an issue.如今,默认相机分辨率要高得多,因此所需的分辨率不是问题。 However, in most cases the resolution is much higher and the image size comes up to 3 MB or so.但是,在大多数情况下,分辨率要高得多,图像大小达到 3 MB 左右。 Because of this, it takes longer to upload the image to server, especially in areas where internet connectivity is poor.因此,将图像上传到服务器需要更长的时间,尤其是在互联网连接较差的地区。

I would like to reduce the size of the image, preferably down to 100 KB or so, without reducing the resolution.我想减小图像的大小,最好减少到 100 KB 左右,而不降低分辨率。

My code for capturing the image through intent is:我通过意图捕获图像的代码是:

     imageHolder = (ImageView)findViewById(R.id.cPhoto78);
     imageHolder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File outputFile1 = new File(getExternalStorageDirectory(), "/APDCLSBM/APDCL1.jpg");
            outputFile1.delete();// Deletes the existing photo
            File photoFile = null;
            try {
                photoFile = createImageFile();

            } catch (IOException ex) {
                Toast.makeText(getBaseContext(),"Error in clicking picture",
                        Toast.LENGTH_SHORT).show();
                finish();
            }
           Intent a = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            if (photoFile != null) {
                Uri outputFileUri = FileProvider.getUriForFile(photoCamera.this,
                        BuildConfig.APPLICATION_ID + ".provider",
                        photoFile);
                a.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(a, 1);
            }

        }
    });

The following function creates the image file and path:以下 function 创建镜像文件和路径:

    private File createImageFile() throws IOException {
    String imageFileName = "APDCL1";
    File storageDir = new File(
            getExternalStorageDirectory() + IMAGE_DIRECTORY);
    File image = new File(storageDir, imageFileName+".jpg");

    currentPhotoPath = image.getAbsolutePath();
    Log.d("path1",currentPhotoPath);
    return image;
}

Please suggest a way请提出一种方法

private File createImageFile() throws IOException {
String imageFileName = "APDCL1";
File storageDir = new File(
        getExternalStorageDirectory() + IMAGE_DIRECTORY);
File image = new File(storageDir, imageFileName+".jpg");
try{
  Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath);
}catch(Exception e){ e.printStacktrace(); return null;}

   File reducedSizeImage = new File.createTempFile("name",".jpg",location);
 try ( FileOutputStream outputStream = new FileOutputStream(reducedSizeImage)) 
 
      {

        image.compress(Bitmap.CompressFormat.JPEG,20, outputStream); // this line will reduce the size , try changing the second argument to adjust to correct size , it ranges 0-100

 
     } catch (Exception e) {
         e.printStackTrace();
         return null;
     }

   return reducedSizeImage // otherwise upload this file
}

Have a good day祝你有美好的一天

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM