com.yalantis.ucrop.UCrop Java Examples

The following examples show how to use com.yalantis.ucrop.UCrop. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PhotoEditorActivity.java    From react-native-photo-editor with Apache License 2.0 6 votes vote down vote up
private void startCropping() {
    System.out.println(selectedImagePath);
    Uri uri = Uri.fromFile(new File(selectedImagePath));
    UCrop.Options options = new UCrop.Options();
    options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
    options.setCompressionQuality(100);
    options.setCircleDimmedLayer(cropperCircleOverlay);
    options.setFreeStyleCropEnabled(freeStyleCropEnabled);
    options.setShowCropGrid(showCropGuidelines);
    options.setHideBottomControls(hideBottomControls);
    options.setAllowedGestures(
            UCropActivity.ALL, // When 'scale'-tab active
            UCropActivity.ALL, // When 'rotate'-tab active
            UCropActivity.ALL  // When 'aspect ratio'-tab active
    );


    UCrop uCrop = UCrop
            .of(uri, Uri.fromFile(new File(this.getTmpDir(this), UUID.randomUUID().toString() + ".jpg")))
            .withOptions(options);

    uCrop.start(this);
}
 
Example #2
Source File: PictureBaseActivity.java    From PictureSelector with Apache License 2.0 6 votes vote down vote up
/**
 * crop
 *
 * @param originalPath
 * @param mimeType
 */
protected void startCrop(String originalPath, String mimeType) {
    if (DoubleUtils.isFastDoubleClick()) {
        return;
    }
    if (TextUtils.isEmpty(originalPath)) {
        ToastUtils.s(this, getString(R.string.picture_not_crop_data));
        return;
    }
    UCrop.Options options = basicOptions();
    if (PictureSelectionConfig.cacheResourcesEngine != null) {
        PictureThreadUtils.executeByIo(new PictureThreadUtils.SimpleTask<String>() {
            @Override
            public String doInBackground() {
                return PictureSelectionConfig.cacheResourcesEngine.onCachePath(getContext(), originalPath);
            }

            @Override
            public void onSuccess(String result) {
                startSingleCropActivity(originalPath, result, mimeType, options);
            }
        });
    } else {
        startSingleCropActivity(originalPath, null, mimeType, options);
    }
}
 
Example #3
Source File: PictureBaseActivity.java    From PictureSelector with Apache License 2.0 6 votes vote down vote up
/**
 * single crop
 *
 * @param originalPath
 * @param cachePath
 * @param mimeType
 * @param options
 */
private void startSingleCropActivity(String originalPath, String cachePath, String mimeType, UCrop.Options options) {
    boolean isHttp = PictureMimeType.isHasHttp(originalPath);
    String suffix = mimeType.replace("image/", ".");
    File file = new File(PictureFileUtils.getDiskCacheDir(getContext()),
            TextUtils.isEmpty(config.renameCropFileName) ? DateUtils.getCreateFileName("IMG_CROP_") + suffix : config.renameCropFileName);
    Uri uri;
    if (!TextUtils.isEmpty(cachePath)) {
        uri = Uri.fromFile(new File(cachePath));
    } else {
        uri = isHttp || SdkVersionUtils.checkedAndroid_Q() ? Uri.parse(originalPath) : Uri.fromFile(new File(originalPath));
    }
    UCrop.of(uri, Uri.fromFile(file))
            .withOptions(options)
            .startAnimationActivity(this, config.windowAnimationStyle != null
                    ? config.windowAnimationStyle.activityCropEnterAnimation : R.anim.picture_anim_enter);
}
 
Example #4
Source File: PictureBaseActivity.java    From PictureSelector with Apache License 2.0 6 votes vote down vote up
/**
 * startMultipleCropActivity
 *
 * @param cutInfo
 * @param options
 */
private void startMultipleCropActivity(CutInfo cutInfo, int count, UCrop.Options options) {
    String path = cutInfo.getPath();
    String mimeType = cutInfo.getMimeType();
    boolean isHttp = PictureMimeType.isHasHttp(path);
    Uri uri;
    if (!TextUtils.isEmpty(cutInfo.getAndroidQToPath())) {
        uri = Uri.fromFile(new File(cutInfo.getAndroidQToPath()));
    } else {
        uri = isHttp || SdkVersionUtils.checkedAndroid_Q() ? Uri.parse(path) : Uri.fromFile(new File(path));
    }
    String suffix = mimeType.replace("image/", ".");
    File file = new File(PictureFileUtils.getDiskCacheDir(this),
            TextUtils.isEmpty(config.renameCropFileName) ? DateUtils.getCreateFileName("IMG_CROP_")
                    + suffix : config.camera || count == 1 ? config.renameCropFileName : StringUtils.rename(config.renameCropFileName));
    UCrop.of(uri, Uri.fromFile(file))
            .withOptions(options)
            .startAnimationMultipleCropActivity(this, config.windowAnimationStyle != null
                    ? config.windowAnimationStyle.activityCropEnterAnimation : R.anim.picture_anim_enter);
}
 
Example #5
Source File: PhotoActivity.java    From CrazyDaily with Apache License 2.0 6 votes vote down vote up
/**
 * 其它配置
 */
private UCrop advancedConfig(@NonNull UCrop uCrop) {
    UCrop.Options options = new UCrop.Options();
    switch (mCropCompressRadioGroup.getCheckedRadioButtonId()) {
        case R.id.photo_crop_compress_png:
            options.setCompressionFormat(Bitmap.CompressFormat.PNG);
            break;
        case R.id.photo_crop_compress_jpg:
        default:
            options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
            break;
    }
    options.setCompressionQuality(getCompressionQuality());
    options.setHideBottomControls(mCropUISetting.isChecked());
    options.setFreeStyleCropEnabled(mCropRatioFreestyle.isChecked());

    options.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
    options.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
    options.setActiveWidgetColor(ContextCompat.getColor(this, R.color.colorPrimary));
    options.setToolbarWidgetColor(ContextCompat.getColor(this, R.color.color_white));
    return uCrop.withOptions(options);
}
 
Example #6
Source File: PhotoActivity.java    From CrazyDaily with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("ResultOfMethodCallIgnored")
private void startCrop(@NonNull Uri uri) {
    String destinationFileName = FileUtil.getFileName(this);
    switch (mCropCompressRadioGroup.getCheckedRadioButtonId()) {
        case R.id.photo_crop_compress_png:
            destinationFileName += ".png";
            break;
        case R.id.photo_crop_compress_jpg:
        default:
            destinationFileName += ".jpg";
            break;
    }
    File parent = new File(getExternalCacheDir(), CacheConstant.CACHE_DIR_IMG);
    if (!parent.exists()) {
        parent.mkdirs();
    }
    Uri destUri = Uri.fromFile(new File(parent, destinationFileName));
    UCrop uCrop = UCrop.of(uri, destUri);
    uCrop = basisConfig(uCrop);
    uCrop = advancedConfig(uCrop);
    uCrop.start(this);
}
 
Example #7
Source File: PhotoPickerActivity.java    From PhotoPicker with Apache License 2.0 6 votes vote down vote up
public void openCropActivity(String path) {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + ".jpg";

    UCrop.Options options = new UCrop.Options();
    options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
    options.setHideBottomControls(true);
    options.setFreeStyleCropEnabled(false);
    options.setCompressionQuality(90);
    options.setToolbarColor(ContextCompat.getColor(this, toolbarColor));
    options.setStatusBarColor(ContextCompat.getColor(this, statusbarColor));

    UCrop.of(Uri.fromFile(new File(path)), Uri.fromFile(new File(getActivity().getCacheDir(), imageFileName)))
            .withAspectRatio(cropX, cropY)
            .withOptions(options)
            .start(PhotoPickerActivity.this);
}
 
Example #8
Source File: MyTool.java    From PhotoOut with Apache License 2.0 6 votes vote down vote up
public static void startCropActivity(Activity context, Uri sourceUri,BasePhotoBuilder builder) {
    CropConfig config = buildCropConfig(builder);
    Uri mDestinationUri = buildUri(builder,false);
    UCrop uCrop = UCrop.of(sourceUri, mDestinationUri);

    uCrop.withAspectRatio(config.aspectRatioX,config.aspectRatioY);
    uCrop.withMaxResultSize(config.maxWidth,config.maxHeight);

    UCrop.Options options = new UCrop.Options();
    options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
    options.setAllowedGestures(UCropActivity.SCALE,UCropActivity.NONE,UCropActivity.NONE);
    options.setCompressionQuality(config.quality);
    // options.setOvalDimmedLayer(config.isOval);
    options.setCircleDimmedLayer(config.isOval);
    options.setShowCropGrid(config.showGridLine);
    options.setHideBottomControls(config.hideBottomControls);
    options.setShowCropFrame(config.showOutLine);
    options.setToolbarColor(config.toolbarColor);
    options.setStatusBarColor(config.statusBarColor);

    uCrop.withOptions(options);

    uCrop.start(context);
}
 
Example #9
Source File: CropUtils.java    From CropUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 注意,调用时data为null的判断
 *
 * @param context
 * @param cropHandler
 * @param requestCode
 * @param resultCode
 * @param data
 */
public static void handleResult(Activity context, CropHandler cropHandler, int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_SELECT_PICTURE) {//第一次,选择图片后返回
            final Uri selectedUri = data.getData();
            if (selectedUri != null) {
                startCropActivity(context, data.getData());
            } else {
                Toast.makeText(context, "Cannot retrieve selected image", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == UCrop.REQUEST_CROP) {//第二次返回,图片已经剪切好

            Uri finalUri = UCrop.getOutput(data);
            cropHandler.handleCropResult(finalUri,config.tag);

        } else if (requestCode == REQUEST_CAMERA) {//第一次,拍照后返回,因为设置了MediaStore.EXTRA_OUTPUT,所以data为null,数据直接就在uri中
            startCropActivity(context, uri);
        }
    }
    if (resultCode == UCrop.RESULT_ERROR) {
        cropHandler.handleCropError(data);
    }

}
 
Example #10
Source File: CropUtils.java    From CropUtils with Apache License 2.0 6 votes vote down vote up
private static void startCropActivity(Activity context, Uri sourceUri) {
     Uri mDestinationUri = buildUri();
     UCrop uCrop = UCrop.of(sourceUri, mDestinationUri);

     uCrop.withAspectRatio(config.aspectRatioX,config.aspectRatioY);
     uCrop.withMaxResultSize(config.maxWidth,config.maxHeight);

     UCrop.Options options = new UCrop.Options();
     options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
     options.setAllowedGestures(UCropActivity.SCALE,UCropActivity.NONE,UCropActivity.NONE);
     options.setCompressionQuality(config.quality);
     options.setOvalDimmedLayer(config.isOval);
     options.setShowCropGrid(config.showGridLine);
     options.setHideBottomControls(config.hideBottomControls);
     options.setShowCropFrame(config.showOutLine);
options.setToolbarColor(config.toolbarColor);
     options.setStatusBarColor(config.statusBarColor);

     uCrop.withOptions(options);

     uCrop.start(context);
 }
 
Example #11
Source File: BoxingUcrop.java    From boxing with Apache License 2.0 6 votes vote down vote up
@Override
public void onStartCrop(Context context, Fragment fragment, @NonNull BoxingCropOption cropConfig,
                        @NonNull String path, int requestCode) {
    Uri uri = new Uri.Builder()
            .scheme("file")
            .appendPath(path)
            .build();
    UCrop.Options crop = new UCrop.Options();
    // do not copy exif information to crop pictures
    // because png do not have exif and png is not Distinguishable
    crop.setCompressionFormat(Bitmap.CompressFormat.PNG);
    crop.withMaxResultSize(cropConfig.getMaxWidth(), cropConfig.getMaxHeight());
    crop.withAspectRatio(cropConfig.getAspectRatioX(), cropConfig.getAspectRatioY());

    UCrop.of(uri, cropConfig.getDestination())
            .withOptions(crop)
            .start(context, fragment, requestCode);
}
 
Example #12
Source File: ImagePickerActivity.java    From Android-Image-Picker-and-Cropping with GNU General Public License v3.0 6 votes vote down vote up
private void cropImage(Uri sourceUri) {
    Uri destinationUri = Uri.fromFile(new File(getCacheDir(), queryName(getContentResolver(), sourceUri)));
    UCrop.Options options = new UCrop.Options();
    options.setCompressionQuality(IMAGE_COMPRESSION);

    // applying UI theme
    options.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimary));
    options.setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimary));
    options.setActiveWidgetColor(ContextCompat.getColor(this, R.color.colorPrimary));

    if (lockAspectRatio)
        options.withAspectRatio(ASPECT_RATIO_X, ASPECT_RATIO_Y);

    if (setBitmapMaxWidthHeight)
        options.withMaxResultSize(bitmapMaxWidth, bitmapMaxHeight);

    UCrop.of(sourceUri, destinationUri)
            .withOptions(options)
            .start(this);
}
 
Example #13
Source File: PicCrop.java    From PicCrop with Apache License 2.0 6 votes vote down vote up
private static void startCropActivity(Activity context, Uri sourceUri) {
     Uri mDestinationUri = buildUri();
     UCrop uCrop = UCrop.of(sourceUri, mDestinationUri);

     uCrop.withAspectRatio(config.aspectRatioX,config.aspectRatioY);
     uCrop.withMaxResultSize(config.maxWidth,config.maxHeight);

     UCrop.Options options = new UCrop.Options();
     options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
     options.setAllowedGestures(UCropActivity.SCALE,UCropActivity.NONE,UCropActivity.NONE);
     options.setCompressionQuality(config.quality);
    // options.setOvalDimmedLayer(config.isOval);
     options.setCircleDimmedLayer(config.isOval);
     options.setShowCropGrid(config.showGridLine);
     options.setHideBottomControls(config.hideBottomControls);
     options.setShowCropFrame(config.showOutLine);
options.setToolbarColor(config.toolbarColor);
     options.setStatusBarColor(config.statusBarColor);

     uCrop.withOptions(options);

     uCrop.start(context);
 }
 
Example #14
Source File: BoxingUcrop.java    From boxing with Apache License 2.0 5 votes vote down vote up
@Override
public Uri onCropFinish(int resultCode, Intent data) {
    if (data == null) {
        return null;
    }
    Throwable throwable = UCrop.getError(data);
    if (throwable != null) {
        return null;
    }
    return UCrop.getOutput(data);
}
 
Example #15
Source File: EditFamilyActivity.java    From Pigeon with MIT License 5 votes vote down vote up
private void handleCropResult(@NonNull Intent result) {
    final Uri resultUri = UCrop.getOutput(result);
    if (resultUri != null) {
        mCurrentPhotoStr = resultUri.getPath();
        mPhotoBitmap = BitmapFactory.decodeFile(resultUri.getPath());
        mFamilyPhoto.setImageBitmap(mPhotoBitmap);
    }


}
 
Example #16
Source File: UpdateUserInfoActivity.java    From Pigeon with MIT License 5 votes vote down vote up
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_SELECT_PICTURE) {
            final Uri selectedUri = data.getData();
            if (selectedUri != null) {
                startCropActivity(data.getData());
            } else {
                ToastUtils.showToast(UpdateUserInfoActivity.this,"类型不匹配");
            }
        } else if (requestCode == UCrop.REQUEST_CROP) {
            handleCropResult(data);
        }
    }
    if (resultCode == UCrop.RESULT_ERROR) {
        handleCropError(data);
    }
}
 
Example #17
Source File: EditFamilyActivity.java    From Pigeon with MIT License 5 votes vote down vote up
private void handleCropError(@NonNull Intent result) {
    final Throwable cropError = UCrop.getError(result);
    if (cropError != null) {
        ToastUtils.showToast(EditFamilyActivity.this, cropError.getMessage());
    } else {
        ToastUtils.showToast(EditFamilyActivity.this, "handleCropError");
    }
}
 
Example #18
Source File: PicturePreviewActivity.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case UCrop.REQUEST_MULTI_CROP:
                // 裁剪数据
                List<CutInfo> list = UCrop.getMultipleOutput(data);
                data.putParcelableArrayListExtra(UCrop.Options.EXTRA_OUTPUT_URI_LIST,
                        (ArrayList<? extends Parcelable>) list);
                // 已选数量
                data.putParcelableArrayListExtra(PictureConfig.EXTRA_SELECT_LIST,
                        (ArrayList<? extends Parcelable>) selectData);
                setResult(RESULT_OK, data);
                finish();
                break;
            case UCrop.REQUEST_CROP:
                if (data != null) {
                    data.putParcelableArrayListExtra(PictureConfig.EXTRA_SELECT_LIST,
                            (ArrayList<? extends Parcelable>) selectData);
                    setResult(RESULT_OK, data);
                }
                finish();
                break;
        }
    } else if (resultCode == UCrop.RESULT_ERROR) {
        Throwable throwable = (Throwable) data.getSerializableExtra(UCrop.EXTRA_ERROR);
        ToastUtils.s(getContext(), throwable.getMessage());
    }
}
 
Example #19
Source File: UpdateUserInfoActivity.java    From Pigeon with MIT License 5 votes vote down vote up
private void handleCropResult(@NonNull Intent result) {
    final Uri resultUri = UCrop.getOutput(result);
    if (resultUri != null) {
        mCurrentPhotoStr = resultUri.getPath();
        mPhotoBitmap = BitmapFactory.decodeFile(resultUri.getPath());
        mCivPhoto.setImageBitmap(mPhotoBitmap);
    }


}
 
Example #20
Source File: PictureSelectorActivity.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case PictureConfig.PREVIEW_VIDEO_CODE:
                if (data != null) {
                    List<LocalMedia> list = data.getParcelableArrayListExtra(PictureConfig.EXTRA_SELECT_LIST);
                    if (list != null && list.size() > 0) {
                        onResult(list);
                    }
                }
                break;
            case UCrop.REQUEST_CROP:
                singleCropHandleResult(data);
                break;
            case UCrop.REQUEST_MULTI_CROP:
                multiCropHandleResult(data);
                break;
            case PictureConfig.REQUEST_CAMERA:
                dispatchHandleCamera(data);
                break;
            default:
                break;
        }
    } else if (resultCode == RESULT_CANCELED) {
        previewCallback(data);
    } else if (resultCode == UCrop.RESULT_ERROR) {
        if (data != null) {
            Throwable throwable = (Throwable) data.getSerializableExtra(UCrop.EXTRA_ERROR);
            if (throwable != null) {
                ToastUtils.s(getContext(), throwable.getMessage());
            }
        }
    }
}
 
Example #21
Source File: FragmentEditImage.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        String path;
        if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
            final Uri resultUri = UCrop.getOutput(data);
            path = AttachFile.getFilePathFromUri(resultUri);

            serCropAndFilterImage(path);

        } else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { // result for crop
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                path = result.getUri().getPath();
                serCropAndFilterImage(path);
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
//                Exception error = result.getError();
            }
        }

    }
 
Example #22
Source File: UpdateUserInfoActivity.java    From Pigeon with MIT License 5 votes vote down vote up
private void handleCropError(@NonNull Intent result) {
    final Throwable cropError = UCrop.getError(result);
    if (cropError != null) {
        ToastUtils.showToast(UpdateUserInfoActivity.this,cropError.getMessage());
    } else {
        ToastUtils.showToast(UpdateUserInfoActivity.this,"handleCropError");
    }
}
 
Example #23
Source File: PictureSelectorActivity.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
/**
 * Preview
 */
private void onPreview() {
    List<LocalMedia> selectedData = mAdapter.getSelectedData();
    List<LocalMedia> medias = new ArrayList<>();
    int size = selectedData.size();
    for (int i = 0; i < size; i++) {
        LocalMedia media = selectedData.get(i);
        medias.add(media);
    }
    if (PictureSelectionConfig.onCustomImagePreviewCallback != null) {
        PictureSelectionConfig.onCustomImagePreviewCallback.onCustomPreviewCallback(getContext(), selectedData, 0);
        return;
    }
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, (ArrayList<? extends Parcelable>) medias);
    bundle.putParcelableArrayList(PictureConfig.EXTRA_SELECT_LIST, (ArrayList<? extends Parcelable>) selectedData);
    bundle.putBoolean(PictureConfig.EXTRA_BOTTOM_PREVIEW, true);
    bundle.putBoolean(PictureConfig.EXTRA_CHANGE_ORIGINAL, config.isCheckOriginalImage);
    bundle.putBoolean(PictureConfig.EXTRA_SHOW_CAMERA, mAdapter.isShowCamera());
    bundle.putString(PictureConfig.EXTRA_IS_CURRENT_DIRECTORY, mTvPictureTitle.getText().toString());
    JumpUtils.startPicturePreviewActivity(getContext(), config.isWeChatStyle, bundle,
            config.selectionMode == PictureConfig.SINGLE ? UCrop.REQUEST_CROP : UCrop.REQUEST_MULTI_CROP);

    overridePendingTransition(config.windowAnimationStyle != null
                    && config.windowAnimationStyle.activityPreviewEnterAnimation != 0
                    ? config.windowAnimationStyle.activityPreviewEnterAnimation : R.anim.picture_anim_enter,
            R.anim.picture_anim_fade_in);
}
 
Example #24
Source File: EditFamilyActivity.java    From Pigeon with MIT License 5 votes vote down vote up
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_SELECT_PICTURE) {
            final Uri selectedUri = data.getData();
            if (selectedUri != null) {
                startCropActivity(data.getData());
            } else {
                ToastUtils.showToast(EditFamilyActivity.this, "类型不匹配");
            }
        } else if (requestCode == UCrop.REQUEST_CROP) {
            handleCropResult(data);
        }
    }
    if (resultCode == UCrop.RESULT_ERROR) {
        handleCropError(data);
    }
}
 
Example #25
Source File: PhotoPickerActivity.java    From PhotoPicker with Apache License 2.0 5 votes vote down vote up
public void openCropActivity(String path) {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
    String imageFileName = "JPEG_" + timeStamp + ".jpg";

    UCrop.Options options = new UCrop.Options();
    options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
    options.setHideBottomControls(true);
    options.setFreeStyleCropEnabled(false);
    options.setCompressionQuality(100);

    options.setToolbarColor(ContextCompat.getColor(this,toolbarColor));
    options.setStatusBarColor(ContextCompat.getColor(this, statusbarColor));

    UCrop.of(Uri.fromFile(new File(path)), Uri.fromFile(new File(getActivity().getCacheDir(), imageFileName)))
            .withAspectRatio(cropX, cropY)
            .withOptions(options)
            .start(PhotoPickerActivity.this);
}
 
Example #26
Source File: ImagePickerActivity.java    From Android-Image-Picker-and-Cropping with GNU General Public License v3.0 5 votes vote down vote up
private void handleUCropResult(Intent data) {
    if (data == null) {
        setResultCancelled();
        return;
    }
    final Uri resultUri = UCrop.getOutput(data);
    setResultOk(resultUri);
}
 
Example #27
Source File: PicCrop.java    From PicCrop with Apache License 2.0 5 votes vote down vote up
/**
 * 注意,调用时data为null的判断
 *
 * @param context
 * @param cropHandler
 * @param requestCode
 * @param resultCode
 * @param data
 */
public static void onActivityResult( int requestCode, int resultCode, Intent data,Activity context, CropHandler cropHandler) {

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_SELECT_PICTURE) {//第一次,选择图片后返回
            final Uri selectedUri = data.getData();
            if (selectedUri != null) {
                startCropActivity(context, data.getData());
            } else {
                Toast.makeText(context, "Cannot retrieve selected image", Toast.LENGTH_SHORT).show();
            }
        } else if (requestCode == UCrop.REQUEST_CROP) {//第二次返回,图片已经剪切好

            Uri finalUri = UCrop.getOutput(data);
            cropHandler.handleCropResult(finalUri,config.tag);

        } else if (requestCode == REQUEST_CAMERA) {//第一次,拍照后返回,因为设置了MediaStore.EXTRA_OUTPUT,所以data为null,数据直接就在uri中
            startCropActivity(context, uri);
        }
    }
    if (resultCode == UCrop.RESULT_ERROR) {
        cropHandler.handleCropError(data);
    }

}
 
Example #28
Source File: SingleMediaActivity.java    From leafpicrevived with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("ResourceAsColor")
private UCrop.Options getUcropOptions() {

    UCrop.Options options = new UCrop.Options();
    options.setCompressionFormat(Bitmap.CompressFormat.PNG);
    options.setCompressionQuality(90);
    options.setActiveControlsWidgetColor(getAccentColor());
    options.setToolbarColor(getPrimaryColor());
    options.setStatusBarColor(isTranslucentStatusBar() ? ColorPalette.getObscuredColor(getPrimaryColor()) : getPrimaryColor());
    options.setCropFrameColor(getAccentColor());
    options.setFreeStyleCropEnabled(true);

    return options;
}
 
Example #29
Source File: PictureActivity.java    From AndroidSamples with Apache License 2.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == UCrop.RESULT_ERROR) {
        mCameraTv.setText(UCrop.getError(data) + "");
        return;
    }
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case RESULT_CODE_1:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    cropRawPhoto(mProviderUri);
                } else {
                    cropRawPhoto(mUri);
                }
                break;
            case RESULT_CODE_2:
                LogUtils.i("onActivityResult: " + data.getData());
                cropRawPhoto(data.getData());
                break;
            case UCrop.REQUEST_CROP:
                LogUtils.i("onActivityResult: " + UCrop.getOutput(data));
                mCameraTv.setText(UCrop.getOutput(data) + "");
                Glide.with(this)
                        .load(UCrop.getOutput(data))
                        .into(mCameraImg);
                break;
            default:
                break;
        }
    }
}
 
Example #30
Source File: PictureActivity.java    From AndroidSamples with Apache License 2.0 5 votes vote down vote up
/**
 * 使用UCrop进行图片剪裁
 *
 * @param uri
 */
public void cropRawPhoto(Uri uri) {

    UCrop.Options options = new UCrop.Options();
    // 修改标题栏颜色
    options.setToolbarColor(getResources().getColor(R.color.colorPrimary));
    // 修改状态栏颜色
    options.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
    // 隐藏底部工具
    options.setHideBottomControls(true);
    // 图片格式
    options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
    // 设置图片压缩质量
    options.setCompressionQuality(100);
    // 是否让用户调整范围(默认false),如果开启,可能会造成剪切的图片的长宽比不是设定的
    // 如果不开启,用户不能拖动选框,只能缩放图片
    options.setFreeStyleCropEnabled(true);
    // 设置图片压缩质量
    options.setCompressionQuality(100);
    // 圆
    options.setCircleDimmedLayer(true);
    // 不显示网格线
    options.setShowCropGrid(false);

    FileUtils.createOrExistsDir(Config.SAVE_REAL_PATH);

    // 设置源uri及目标uri
    UCrop.of(uri, Uri.fromFile(new File(Config.SAVE_REAL_PATH, System.currentTimeMillis() + ".jpg")))
            // 长宽比
            .withAspectRatio(1, 1)
            // 图片大小
            .withMaxResultSize(200, 200)
            // 配置参数
            .withOptions(options)
            .start(this);
}