【问题标题】:Reduce image size without changing resolution android studio在不改变分辨率的情况下减小图像大小 android studio
【发布时间】:2020-06-25 21:36:31
【问题描述】:

在我的 Android 应用程序中,我使用相机意图单击照片并将其上传到我的服务器进行处理。但是,要求图像的分辨率应大于 500X500。

如今,默认相机分辨率要高得多,因此所需的分辨率不是问题。但是,在大多数情况下,分辨率要高得多,图像大小达到 3 MB 左右。因此,将图像上传到服务器需要更长的时间,尤其是在互联网连接较差的地区。

我想在不降低分辨率的情况下减小图像的大小,最好减小到 100 KB 左右。

我通过意图捕获图像的代码是:

     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);
            }

        }
    });

以下函数创建图像文件和路径:

    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;
}

请推荐一个方法

【问题讨论】:

  • 你的意思是“减小文件大小”吗?
  • The following function creates the image file and path:。不,幸运的是它没有。它只创建一个 File 对象。你知道一条路。但是还没有图像文件。这留给相机应用程序。
  • 拍摄照片后,将文件加载到位图中(如果可能的话)。然后使用较低的质量百分比再次压缩为 jpg 文件。
  • "Error in clicking picture" 你可以做得更好。将ex.getMessage() 添加到字符串中。
  • finish();。那太苛刻了。替换为return;

标签: android image


【解决方案1】:
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
}

祝你有美好的一天

【讨论】:

  • 您的代码有效。好的。 (好吧,如果有异常,您应该返回 null。您继续两次,就好像什么都没发生一样)。但是太复杂了。取消 ByteArrayOutputStream。可以直接压缩到outputStream。
  • @blackapps 现在可以了吗?我只是尽量少碰他的代码,然后添加进去
  • 我仍然看到 ByteArrayOutputStream。所以没有。
  • @blackapps 现在?
  • 是的,现在是时候了。
猜你喜欢
  • 2014-05-08
  • 1970-01-01
  • 2011-08-23
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 2011-01-16
相关资源
最近更新 更多