【问题标题】:Have a user crop an image让用户裁剪图像
【发布时间】:2015-11-05 23:22:59
【问题描述】:

我目前有它,用户从图库中拍摄图像。然后将其转换为位图并显示在图像视图中并发送到我的服务器。我目前对其进行裁剪的方式如下。但是,我希望用户有一个可以移动的 300x300 框,当他们按下上传时,它将上传 300x300 视图中的内容

 if (bitmap.getWidth() >= bitmap.getHeight()){

                    bitmap = Bitmap.createBitmap(
                            bitmap,
                            0,
                            0,
                            bitmap.getHeight(),
                            bitmap.getHeight()
                    );

                }else{

                    bitmap = Bitmap.createBitmap(
                            bitmap,
                            0,
                            0,
                            bitmap.getWidth(),
                            bitmap.getWidth()
                    );
                }

这是我的第一个应用程序,所以我仍在努力。提前感谢您的时间和耐心。

【问题讨论】:

    标签: android bitmap crop


    【解决方案1】:

    这是一个裁剪所需大小图像的示例函数。请检查它。您可以从 onActivityResult() 调用此“performCrop()”函数。

    public void performCrop() {
    
            try {
                // call the standard crop action intent (the user device may not
                // support it)
                Intent cropIntent = new Intent("com.android.camera.action.CROP");
                // indicate image type and Uri
                cropIntent.setDataAndType(selectedImageUri, "image/*");
                // set crop properties
                cropIntent.putExtra("crop", "true");
                // indicate aspect of desired crop
                cropIntent.putExtra("aspectX", 1);
                cropIntent.putExtra("aspectY", 1);
                // indicate output X and Y
                cropIntent.putExtra("outputX", 300); //your desired size here 
                cropIntent.putExtra("outputY", 300); //your desired size here
                // retrieve data on return
                cropIntent.putExtra("return-data", true);
                File f = createNewFile("CROP_");
                try {
                    f.createNewFile();
                } catch (IOException ex) {
    
                } catch (NullPointerException ex) {
    
                }
    
                CropImagedUri = Uri.fromFile(f);
                cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, CropImagedUri);
    
                // start the activity - we handle returning in onActivityResult
                startActivityForResult(cropIntent, 2);
            } catch (ActivityNotFoundException anfe) {
                // display an error message
                String errorMessage = "Whoops - your device doesn't support the crop action!";
                Toast toast = Toast
                        .makeText(this, errorMessage, Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                if (requestCode == 1) {
                    selectedImageUri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                    System.out.println("Image Path : " + selectedImagePath);
                    PerformCrop();
                    // Img.setImageURI(selectedImageUri);
                } else if (requestCode == 2) {
                    CropImagePath = getRealPathFromURI(CropImagedUri);
                }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 2015-08-10
      • 2019-10-18
      • 2017-05-22
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多