【问题标题】:How to resize a photo in android?如何在android中调整照片的大小?
【发布时间】:2023-03-25 01:21:01
【问题描述】:

我实际上是 android 新手,但我设法用我的应用程序拍照,这就是我拍照并保存它的方式......问题是我需要在将它保存到手机之前调整它的大小。 ..但我不知道该怎么做..我用谷歌搜索了我的问题,但我发现的唯一问题是位图图片,我猜这不是我的情况..

这是我用来拍照的代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"GUASTO" + System.currentTimeMillis() + ".jpg"); /

intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);

startActivityForResult(intent, SCATTA_FOTO);

谢谢!!

【问题讨论】:

标签: android image resize


【解决方案1】:
public static int PIC_CROP=81;

   Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
                //indicate image type and Uri
            cropIntent.setDataAndType(ImageUri, "image/*");
                //set crop properties
                //indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
                //indicate output X and Y
            cropIntent.putExtra("outputX", 640);
            cropIntent.putExtra("outputY", 640);
                //retrieve data on return
            cropIntent.putExtra("return-data", true);
                //start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);

在onActivityResult()中

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
         if(requestCode==PIC_CROP)
        {
             if(resultCode==RESULT_OK)
             {
             Bundle extras = data.getExtras();
                if (extras != null) {               
                    Bitmap bmp = extras.getParcelable("data");
                    saveCropPhoto(bmp);
                }
              Toast.makeText(getApplicationContext(), "picture cropped",Toast.LENGTH_SHORT).show();
             }
        }

        }

saveCropPhoto() 方法

public void saveCropPhoto(Bitmap bmp)
        {
        Toast.makeText(getApplicationContext(), "in save",Toast.LENGTH_SHORT).show();
        String dir = Environment.getExternalStorageDirectory().toString() + "/folderName/"; 
        File newdir = new File(dir); 
        newdir.mkdirs();
        FileOutputStream out = null;
        Calendar c = Calendar.getInstance();
        String date = fromInt(c.get(Calendar.MONTH))
                    + fromInt(c.get(Calendar.DAY_OF_MONTH))
                    + fromInt(c.get(Calendar.YEAR))
                    + fromInt(c.get(Calendar.HOUR_OF_DAY))
                    + fromInt(c.get(Calendar.MINUTE))
                    + fromInt(c.get(Calendar.SECOND));
        File imageFileName = new File(newdir, "crop_"+date.toString() + ".jpg");
        try
        {
         out = new FileOutputStream(imageFileName);
         bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
         out.flush();
         out.close();
         out = null;
         if(tempFile.exists())
         tempFile.delete();
        } catch (Exception e)
        {
        e.printStackTrace();
        }
        }

【讨论】:

猜你喜欢
  • 2017-06-14
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 2014-02-13
  • 2017-12-22
  • 1970-01-01
相关资源
最近更新 更多