从相机拍摄时裁剪图像问题

时间:2022-08-27 10:41:44

Below is my related code snippet where in it redirects to the device's cropping app after the picture has been shot from the camera. I usually followed this example. But during the phase/moment between the picture taken from camera and making that available in crop app, it shows continuously loading and makes my app terribly unresponsive. If this happens at least once, every time I open the app, it just shows loading.

下面是我的相关代码片段,其中的内容在从相机拍摄照片后重定向到设备的裁剪应用程序。我通常都遵循这个例子。但是在从相机拍摄的照片与在裁剪应用程序中提供照片之间的阶段/时刻,它会显示持续加载并使我的应用程序非常无响应。如果这种情况至少发生一次,那么每次打开应用程序时,它都会显示加载情况。

This issue doesn't occur in one scenario--> When I don't move my device(i.e., only when no screen orientation happens). Can some one please suggest me to avoid this issue?

在一种情况下不会发生此问题 - >当我不移动我的设备时(即,仅在没有屏幕方向发生时)。有人可以建议我避免这个问题吗?

public void shotFromCamera(View view)
{
    Intent intent    = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageCaptureUri);

                try {
                    intent.putExtra("return-data", true);

                    startActivityForResult(intent, PICK_FROM_CAMERA);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }
}

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

        if(resultCode==RESULT_OK && requestCode == PICK_FROM_CAMERA){

            doCrop();
                 } else if(resultCode == RESULT_OK && requestCode == CROP_FROM_CAMERA)
        {
            Bundle extras = data.getExtras();

            if (extras != null) {               
                Bitmap photoBitmap = extras.getParcelable("data");

                imageView.setImageBitmap(photoBitmap);
                if(mImageCaptureUri != null)
                {
                File f = new File(mImageCaptureUri.getPath());
                if (f.exists()) f.delete();
                }

                mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+ "/MyApp",
                           "avatar" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
                File file = new File(mImageCaptureUri.getPath()); 

                    selectedImagePath = mImageCaptureUri.getPath();
                    Log.d("pic1 ","pic to be created: "+selectedImagePath);
                    OutputStream fOut = null;

                    try {
                        fOut = new FileOutputStream(file);
                        photoBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                        fOut.flush();
                        fOut.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch(IOException e)
                    {
                        e.printStackTrace();
                    }       

            }


        }
}

    private void doCrop() {
            final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setType("image/*");

            List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );

            int size = list.size();

            if (size == 0) {            
                Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();

                return;
            } else {
                intent.setData(imageCaptureUri);

                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("scale", true);
                intent.putExtra("return-data", true);

                if (size == 1) {
                    Intent i        = new Intent(intent);
                    ResolveInfo res = list.get(0);

                    i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                    startActivityForResult(i, CROP_FROM_CAMERA);
                }
   }

To clarify my issue, I had run the source code in that example as it is, the same issue happens. I had run other couple of examples which redirect to the cropping app. The same issue happens while changing the orientation during the above explained phase.

为了澄清我的问题,我在该示例中运行了源代码,同样的问题发生了。我已经运行了其他几个重定向到裁剪应用程序的示例。在上述解释阶段期间改变方向时会发生同样的问题。

P.S : If anyone has working example which surely works even on orientation changes, I would be glad to accept it as an answer.

P.S:如果有人有工作的例子,即使在方向改变方面确实有效,我也很乐意接受它作为答案。

2 个解决方案

#1


1  

You can go with this-

你可以用这个 -

  1. get the Bitmap from specified URI, i.e a full sized Image using

    从指定的URI获取Bitmap,即使用完整大小的Image

    photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(imageCaptureUri));

    photo = android.provider.MediaStore.Images.Media.getBitmap(cr,Uri.fromFile(imageCaptureUri));

  2. Use Bitmap.CompressFormat.PNG while compressing the Image

    在压缩Image时使用Bitmap.CompressFormat.PNG

  3. create a scaled bitmap using

    使用创建缩放位图

    photo= Bitmap.createScaledBitmap(background, YourWidth, YourHeight, true);

    photo = Bitmap.createScaledBitmap(background,YourWidth,YourHeight,true);

  4. This is your Scaled (Cropped) Image.

    这是您的缩放(裁剪)图像。

#2


0  

check the value of imageCaptureUri != null before line intent.setData(imageCaptureUri); in doCrop()

在行intent.setData(imageCaptureUri)之前检查imageCaptureUri!= null的值;在doCrop()中

Edit

First save the camera result in file and pass the file for cropping app

首先将相机结果保存到文件中并传递文件以进行裁剪应用

intent.setData(Uri.fromFile(new File(path)));

#1


1  

You can go with this-

你可以用这个 -

  1. get the Bitmap from specified URI, i.e a full sized Image using

    从指定的URI获取Bitmap,即使用完整大小的Image

    photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(imageCaptureUri));

    photo = android.provider.MediaStore.Images.Media.getBitmap(cr,Uri.fromFile(imageCaptureUri));

  2. Use Bitmap.CompressFormat.PNG while compressing the Image

    在压缩Image时使用Bitmap.CompressFormat.PNG

  3. create a scaled bitmap using

    使用创建缩放位图

    photo= Bitmap.createScaledBitmap(background, YourWidth, YourHeight, true);

    photo = Bitmap.createScaledBitmap(background,YourWidth,YourHeight,true);

  4. This is your Scaled (Cropped) Image.

    这是您的缩放(裁剪)图像。

#2


0  

check the value of imageCaptureUri != null before line intent.setData(imageCaptureUri); in doCrop()

在行intent.setData(imageCaptureUri)之前检查imageCaptureUri!= null的值;在doCrop()中

Edit

First save the camera result in file and pass the file for cropping app

首先将相机结果保存到文件中并传递文件以进行裁剪应用

intent.setData(Uri.fromFile(new File(path)));