簡體   English   中英

從圖像選擇器Activiy中裁剪圖像

[英]Square crop an image from image picker Activiy

我有兩種為我挑選圖片的方法。 選擇圖片后,我想做類似Facebook個人資料圖片方形裁切的操作。 如果有簡單的方法(例如,將附加內容放入意圖(“ crop”,“ true”),(“ aspectX / Y”,1)等),然后就在徘徊。 目前,我正在嘗試附加意圖,但無法使其正常工作。

常量.TAKE_CAMERA_PICTURE是1000常量.SELECT_PICTURE_ACTIVITY_REQUEST_CODE是1001

private void takeCameraPhoto() {
    mPhotoHelper = PhotoHelper.recycle(mPhotoHelper);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri photoUri = Uri.fromFile(mPhotoHelper.getPhotoFile());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
    startActivityForResult(intent, Constants.TAKE_CAMERA_PICTURE);
}

public void importPhotoAlbum() {
    mPhotoHelper = PhotoHelper.recycle(mPhotoHelper);
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, Constants.SELECT_PICTURE_ACTIVITY_REQUEST_CODE);
}

提前致謝!

我正在為此目的使用jdamcd / android-crop庫。 只需在您的項目中將其添加為庫即可。現在要裁剪圖像,只需寫一行,即

Crop.of(source, dest).asSquare().start(this, CropCode);

在這里, 是圖像文件的Uri, 目標是要保存圖像的目標uri

它將為您完成所有工作。還有其他裁剪方法。 讓我知道您是否有任何困難:-)

如果要使用純Android SDK,請將Bitmap類與createBitmap方法一起使用,在此處檢查文檔,並檢查以下示例:

Bitmap croppedImage = Bitmap.createBitmap(sourceImage, startX, startY, destinationWidth, destinationHeight);

如果要使用第三方SDK,請查看此庫Cropper Android小部件,用於裁剪和旋轉圖像 ,我認為它將完成您想要的操作,請使用以下示例查看其Wiki頁面:

1-將CropImageView添加到您的布局XML文件

<com.edmodo.cropper.CropImageView 
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/CropImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

2-您可以使用提供的CropImageView方法以編程方式修改屬性:

CropImageView cropImageView = (CropImageView) findViewById(R.id.CropImageView);
cropImageView.setAspectRatio(5, 10);
cropImageView.setFixedAspectRatio(true);
cropImageView.setGuidelines(1);

3-要檢索“裁剪器”窗口中包含的圖像,請使用提供的方法getCroppedImage()檢索裁剪后的圖像的位圖。

croppedImage = cropImageView.getCroppedImage();
ImageView croppedImageView = (ImageView) findViewById(R.id.croppedImageView);
croppedImageView.setImageBitmap(croppedImage);

希望有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM