Мне нужна помощь в обрезке потока растрового изображения, которое выбирается через кэш LRU, моя задача - обрезать изображения по определенным координатам, которые должны быть определены один раз, а затем все изображения должны быть обрезаны по этим самым координатам.
В настоящее время мы использовали DragReactangle
в андроиде, но он не обрезается точно.
Вот мои три метода:
private static Bitmap ScaleDownBitmap(Bitmap originalImage, Boolean filter) {
float ratio = Math.min((float) mWidth_ImageView / originalImage.getWidth(),
(float) mWidth_ImageView / originalImage.getHeight());
int width = Math.round(ratio * (float) originalImage.getWidth());
int height = Math.round(ratio * (float) originalImage.getHeight());
return Bitmap.createScaledBitmap(originalImage, width, height, filter);
}
public static void initCroppingParametersFromPrefs() {
sKeyRectX = Prefs.getInt(
CommonDataApplication.getInstance().getApplicationContext().getString(R.string.key_x),
0);
sKeyRectY = Prefs.getInt(
CommonDataApplication.getInstance().getApplicationContext().getString(R.string.key_y),
0);
sKeyRectWidth = Prefs.getInt(
CommonDataApplication.getInstance().getApplicationContext().getString(R.string.Key_width),
0);
sKeyRectHeight = Prefs.getInt(
CommonDataApplication.getInstance().getApplicationContext().getString(R.string.key_height),
0);
}
public static Bitmap getCroppedCameraStreamBitmap(Bitmap mBitmap) {
if ((sKeyRectX <= 0 && sKeyRectY <= 0 && sKeyRectWidth <= 0 && sKeyRectHeight <= 0)) {
//If items are not initialized then we return the original bitmap
return mBitmap;
}
float ratio = (float) Math.min((float) mWidth_ImageView / mBitmap.getWidth(),
(float) mWidth_ImageView / mBitmap.getHeight());
/**
* Do sanity checks i.e. y+height should be less than bitmap and x+width less than width of bitmap
*/
int width = (int) (sKeyRectWidth / ratio);
int height = (int) (sKeyRectHeight / ratio);
int start_x_pixel = (int) (sKeyRectX / ratio);
int start_y_pixel = (int) (sKeyRectY / ratio);
start_x_pixel = start_x_pixel <= mBitmap.getWidth() ? start_x_pixel : mBitmap.getWidth();
start_y_pixel = start_y_pixel <= mBitmap.getHeight() ? start_y_pixel : mBitmap.getHeight();
if (width + start_x_pixel > mBitmap.getWidth()) {
width = mBitmap.getWidth() - start_x_pixel;
}
if (height + start_y_pixel > mBitmap.getHeight()) {
height = mBitmap.getHeight() - start_y_pixel;
}
return Bitmap
.createBitmap(mBitmap, start_x_pixel, start_y_pixel,
width,
height
);
}
в Основном занятии, которое я делаювот так
mDragRectView = findViewById(R.id.dragRect);
final LRUCache lruCache = CommonDataApplication.getInstance().getStateManager()
.getmLRUCacheForCamera();
//final int key = lruCache.getLatestFrameKey();
if (lruCache == null) {
return;
}
//See if we can get the bitmap and then we start to process it.
try {
final Bitmap latest_bitmap = lruCache.grabLatestFrameFromCache().getOriginalBitmap();
if (latest_bitmap != null) {
mDragRectView.setImageBitmap(ScaleDownBitmap(latest_bitmap, true));
}
mDragRectView.setOnUpCallback(new DragRectangleView.OnUpCallback() {
@Override
public void onRectFinished(final Rect rect) {
//points which is Required for Cropping Rectangle
Log.d("Rect need for bitmap",
rect.left + " " + rect.top + " " + (rect.right - rect.left) + " " + (rect.bottom
- rect.top) + " ");
Prefs.setInt(
CommonDataApplication.getInstance().getApplicationContext().getString(R.string.key_x),
rect.left);
Prefs.setInt(
CommonDataApplication.getInstance().getApplicationContext().getString(R.string.key_y),
rect.top);
Prefs.setInt(CommonDataApplication.getInstance().getApplicationContext()
.getString(R.string.Key_width), rect.right - rect.left);
Prefs.setInt(CommonDataApplication.getInstance().getApplicationContext()
.getString(R.string.key_height), rect.bottom
- rect.top);
}
});
} catch (NullPointerException e) {
}
Пожалуйста, кто-нибудь может мне помочь с этим кодом, что я здесь делаю неправильно?Или лучшее решение?