【问题标题】:How to pick image from camera and gallery , crop it and set to image view如何从相机和图库中选择图像,裁剪并设置为图像视图
【发布时间】:2017-06-25 12:03:47
【问题描述】:

请帮助我,到目前为止,我已经编写了一个代码,可以从相机和图库中选择图像并显示它,但我发现很难裁剪它。我正在尝试使用一个名为 android imagecropper 的第三方 android 库,但发现它很难实现。我已经玩过代码,但没有任何有用的东西出现。换句话说,我正在尝试实现在 whasap 中获得的效果,您可以在其中单击按钮上传个人资料图片,将其设置为裁剪,然后显示图像的圆形视图。这是 android 库 android image cropper 的链接。任何帮助将不胜感激谢谢大家。

这是我的java代码

public class MainActivity extends Activity {

    private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
    private Button btnSelect;
    private ImageView ivImage;
    private String userChoosenTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSelect = (Button) findViewById(R.id.btnSelectPhoto);
        btnSelect.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                selectImage();
            }
        });
        ivImage = (ImageView) findViewById(R.id.ivImage);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if(userChoosenTask.equals("Take Photo"))
                        cameraIntent();
                    else if(userChoosenTask.equals("Choose from Library"))
                        galleryIntent();
                } else {
                    //code for deny
                }
                break;
        }
    }

    private void selectImage() {
        final CharSequence[] items = { "Take Photo", "Choose from Library",
                "Cancel" };

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Add Photo!");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                boolean result=Utility.checkPermission(MainActivity.this);

                if (items[item].equals("Take Photo")) {
                    userChoosenTask ="Take Photo";
                    if(result)
                        cameraIntent();

                } else if (items[item].equals("Choose from Library")) {
                    userChoosenTask ="Choose from Library";
                    if(result)
                        galleryIntent();

                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

    // Ezphoto picker  in action

    private void galleryIntent()
    {
        EZPhotoPickConfig config = new EZPhotoPickConfig();
        config.photoSource = PhotoSource.GALERY;
        config.needToExportThumbnail = true;
        config.exportingThumbSize = 200;
        config.exportingSize = 1000;
        EZPhotoPick.startPhotoPickActivity(MainActivity.this, config);
    }

    private void cameraIntent()
    {
        EZPhotoPickConfig config = new EZPhotoPickConfig();
        config.photoSource = PhotoSource.CAMERA;
        config.exportingSize = 1000;
        EZPhotoPick.startPhotoPickActivity(MainActivity.this, config);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != RESULT_OK){
            return;
        }

        if(requestCode == EZPhotoPick.PHOTO_PICK_REQUEST_CODE){
            try {
                Bitmap pickedPhoto = new EZPhotoPickStorage(this).loadLatestStoredPhotoBitmapThumbnail();
                ivImage.setImageBitmap(pickedPhoto);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

这是我的 xml 代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp" >

        <Button
            android:id="@+id/btnSelectPhoto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Select Photo" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp" >

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: java android xml imageview


    【解决方案1】:

    来源:Crop image in android

    这里是代码

    private void performCrop(Uri picUri) {
        try {
            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            // indicate image type and Uri
            cropIntent.setDataAndType(picUri, "image/*");
            // set crop properties here
            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", 128);
            cropIntent.putExtra("outputY", 128);
            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, PIC_CROP);
        }
        // respond to users whose devices do not support the crop action
        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();
        }
    }
    

    声明:

    final int PIC_CROP = 1;
    

    在顶部。

    在onActivity结果方法中,写下这段代码:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == PIC_CROP) {
            if (data != null) {
                // get the returned data
                Bundle extras = data.getExtras();
                // get the cropped bitmap
                Bitmap selectedBitmap = extras.getParcelable("data");
    
                imgView.setImageBitmap(selectedBitmap);
            }
        }
    }
    

    来源:Crop image in android

    其他有用的链接:

    希望对您有所帮助!祝你好运!

    【讨论】:

    • @S Kharbanda 请再混淆我在哪里声明 final int PIC_CROP = 1;
    • 在声明 private int REQUEST_CAMERA = 0, SELECT_FILE = 1; 的顶部声明 int PIC_CROP = 1;。就像那样,但这只是 1 个变量而不是 2 个。
    • 没问题,如果您能投票并接受我的回答,我将不胜感激。谢谢!
    【解决方案2】:

    你可以试试这个库来裁剪图像

    https://github.com/jdamcd/android-crop

    如果你喜欢我的建议,请投票。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-21
      • 2016-11-19
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 2018-10-24
      • 2018-01-17
      相关资源
      最近更新 更多