Cropping a Stream of Bitmap at a certain coordinates everytime that are coming from the LRU cache?












0















I need help in cropping a Stream of a bitmap that is being fetched through LRU cache, my job is to Crop the Images at certain coordinates that should be defined once and then all the images should be cropped at that very coordinates.



Currently, we have used the DragReactangle in android but its not cropping accurately.



Heres are my three methods:



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
);

}


in The Main Activity i am doing like this



 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) {

}


Please, can anyone help me with this code what I am doing wrong here? Or a better solution?










share|improve this question

























  • I know its too long question but can anyone help me with this?

    – vivek panchal
    Nov 14 '18 at 7:25
















0















I need help in cropping a Stream of a bitmap that is being fetched through LRU cache, my job is to Crop the Images at certain coordinates that should be defined once and then all the images should be cropped at that very coordinates.



Currently, we have used the DragReactangle in android but its not cropping accurately.



Heres are my three methods:



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
);

}


in The Main Activity i am doing like this



 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) {

}


Please, can anyone help me with this code what I am doing wrong here? Or a better solution?










share|improve this question

























  • I know its too long question but can anyone help me with this?

    – vivek panchal
    Nov 14 '18 at 7:25














0












0








0








I need help in cropping a Stream of a bitmap that is being fetched through LRU cache, my job is to Crop the Images at certain coordinates that should be defined once and then all the images should be cropped at that very coordinates.



Currently, we have used the DragReactangle in android but its not cropping accurately.



Heres are my three methods:



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
);

}


in The Main Activity i am doing like this



 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) {

}


Please, can anyone help me with this code what I am doing wrong here? Or a better solution?










share|improve this question
















I need help in cropping a Stream of a bitmap that is being fetched through LRU cache, my job is to Crop the Images at certain coordinates that should be defined once and then all the images should be cropped at that very coordinates.



Currently, we have used the DragReactangle in android but its not cropping accurately.



Heres are my three methods:



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
);

}


in The Main Activity i am doing like this



 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) {

}


Please, can anyone help me with this code what I am doing wrong here? Or a better solution?







android crop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 10:53









André Sousa

1,1581818




1,1581818










asked Nov 13 '18 at 10:48









vivek panchalvivek panchal

62




62













  • I know its too long question but can anyone help me with this?

    – vivek panchal
    Nov 14 '18 at 7:25



















  • I know its too long question but can anyone help me with this?

    – vivek panchal
    Nov 14 '18 at 7:25

















I know its too long question but can anyone help me with this?

– vivek panchal
Nov 14 '18 at 7:25





I know its too long question but can anyone help me with this?

– vivek panchal
Nov 14 '18 at 7:25












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279328%2fcropping-a-stream-of-bitmap-at-a-certain-coordinates-everytime-that-are-coming-f%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279328%2fcropping-a-stream-of-bitmap-at-a-certain-coordinates-everytime-that-are-coming-f%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation