在 android 中运行时裁剪图像

发布于 2024-11-26 19:42:11 字数 192 浏览 3 评论 0原文

我已经使用 Intent 在活动的 ImageView 中从 SD 卡中选择了图像。现在我想显示一个固定大小的移动矩形,即我们必须使用手势以及我们想要的图像的任何部分,然后我们就可以对其进行裁剪。我们怎样才能做到这一点?这对我来说真的很难做到吗? 请帮我做这件事吗?

更新-->我已经能够带来矩形,但在裁剪和保存所选部分时遇到问题。如何做到这一点?

I have already selected an image from SD card in my activity's ImageView using Intent.and now I want to show a fixed size moving Rectangle i.e. we have to use gesture and whatever portion of the image we want,then we are able to crop that.How can we do that?Its really tough for me to do?
Please help me in doing that?

Update-->I have been able to bring the rectangle and I m getting problem in cropping and saving that selected part.How to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2024-12-03 19:42:11

好的,吉坦贾利。尝试此代码,这将打开图库,您可以选择要裁剪的照片,它将存储名称以 apple 开头的内容,您可以在活动中看到裁剪后的图像

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        photoPickerIntent.putExtra("crop","true");
        photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
        photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
         startActivityForResult(photoPickerIntent, 1);

    }

       private Uri getTempFile() {
       if (isSDCARDMounted()) {
           String f;
           muri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                    "apple_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
       //File f = new File(Environment.getExternalStorageDirectory(),"titus1.jpg");
       try {
        f=muri.getPath();
       } catch (Exception e) {

       }
       return muri;
       } else {
       return null;
       }
      }
   private boolean isSDCARDMounted(){
       String status = Environment.getExternalStorageState();
       if (status.equals(Environment.MEDIA_MOUNTED))
       return true;
       return false;
       }
   protected void onActivityResult(int requestCode, int resultCode,
           Intent imageReturnedIntent) {
       super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

       switch (requestCode) {
       case 1:
           if (resultCode == RESULT_OK) {  
           String filePath= muri.getPath();
           Log.e("path", "filePath");
           Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();

           Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
           image = (ImageView)findViewById(R.id.image);
           image.setImageBitmap(selectedImage);


        }
        }
    }

ok geetanjali. try this code this will open gallery and you can pick a photo to crop, it will store with name starts from apple, you can see cropped image in your activity

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        photoPickerIntent.putExtra("crop","true");
        photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
        photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
         startActivityForResult(photoPickerIntent, 1);

    }

       private Uri getTempFile() {
       if (isSDCARDMounted()) {
           String f;
           muri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                    "apple_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
       //File f = new File(Environment.getExternalStorageDirectory(),"titus1.jpg");
       try {
        f=muri.getPath();
       } catch (Exception e) {

       }
       return muri;
       } else {
       return null;
       }
      }
   private boolean isSDCARDMounted(){
       String status = Environment.getExternalStorageState();
       if (status.equals(Environment.MEDIA_MOUNTED))
       return true;
       return false;
       }
   protected void onActivityResult(int requestCode, int resultCode,
           Intent imageReturnedIntent) {
       super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

       switch (requestCode) {
       case 1:
           if (resultCode == RESULT_OK) {  
           String filePath= muri.getPath();
           Log.e("path", "filePath");
           Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();

           Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
           image = (ImageView)findViewById(R.id.image);
           image.setImageBitmap(selectedImage);


        }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文