我正在尝试裁剪图像以及其背后的背景,以满足我需要做的特定要求。我正在使用流行的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);
}
}
}