Обрезка изображения Android вместе с фоном
Я пытаюсь обрезать изображение вместе с фоном за ним для конкретного требования, которое мне нужно выполнить. Я использую популярную библиотеку uCrop и немного изменил ее, чтобы можно было обрезать изображения за пределами полей. Однако когда я кадрирую изображение, фон не отображается. Я прикрепил изображение, чтобы отобразить мое требование. Часть белого фона должна быть на итоговом изображении. Я попытался настроить следующие методы в BitmapCropTask.java, особенно offsetX и offsetY:
private boolean crop(float resizeScale) throws IOException {
ExifInterface originalExif = new ExifInterface(mImageInputPath);
cropOffsetX = Math.round((mCropRect.left - mCurrentImageRect.left) / mCurrentScale);
cropOffsetY = Math.round((mCropRect.top - mCurrentImageRect.top) / mCurrentScale);
mCroppedImageWidth = Math.round(mCropRect.width() / mCurrentScale);
mCroppedImageHeight = Math.round(mCropRect.height() / mCurrentScale);
// cropOffsetX = Math.round((mCropRect.left - mCurrentImageRect.left) / mCurrentScale);
// cropOffsetY = Math.round((mCropRect.top - mCurrentImageRect.top) / mCurrentScale);
// mCroppedImageWidth = Math.round(mCropRect.width() / mCurrentScale);
// mCroppedImageHeight = Math.round(mCropRect.height() / mCurrentScale);
boolean shouldCrop = shouldCrop(mCroppedImageWidth, mCroppedImageHeight);
Log.i(TAG, "Should crop: " + shouldCrop);
if (shouldCrop) {
boolean cropped = cropCImg(mImageInputPath, mImageOutputPath,
cropOffsetX, cropOffsetY, mCroppedImageWidth, mCroppedImageHeight,
mCurrentAngle, resizeScale, mCompressFormat.ordinal(), mCompressQuality,
mExifInfo.getExifDegrees(), mExifInfo.getExifTranslation());
if (cropped && mCompressFormat.equals(Bitmap.CompressFormat.JPEG)) {
ImageHeaderParser.copyExif(originalExif, mCroppedImageWidth, mCroppedImageHeight, mImageOutputPath);
}
return cropped;
} else {
FileUtils.copyFile(mImageInputPath, mImageOutputPath);
return false;
}
}
private boolean shouldCrop(int width, int height) {
int pixelError = 1;
pixelError += Math.round(Math.max(width, height) / 1000f);
return (mMaxResultImageSizeX > 0 && mMaxResultImageSizeY > 0)
|| Math.abs(mCropRect.left - mCurrentImageRect.left) > pixelError
|| Math.abs(mCropRect.top - mCurrentImageRect.top) > pixelError
|| Math.abs(mCropRect.bottom - mCurrentImageRect.bottom) > pixelError
|| Math.abs(mCropRect.right - mCurrentImageRect.right) > pixelError
|| mCurrentAngle != 0;
}
@SuppressWarnings("JniMissingFunction")
native public static boolean
cropCImg(String inputPath, String outputPath,
int left, int top, int width, int height,
float angle, float resizeScale,
int format, int quality,
int exifDegrees, int exifTranslation) throws IOException, OutOfMemoryError;
@Override
protected void onPostExecute(@Nullable Throwable t) {
if (mCropCallback != null) {
if (t == null) {
Uri uri = Uri.fromFile(new File(mImageOutputPath));
mCropCallback.onBitmapCropped(uri, cropOffsetX, cropOffsetY, mCroppedImageWidth, mCroppedImageHeight);
} else {
mCropCallback.onCropFailure(t);
}
}
}
Но пока не повезло, возможно ли это с помощью библиотеки uCrop? А если нет, какую альтернативную библиотеку вы бы посоветовали использовать? Благодарю.