Crop Bitmap with rectangle dimensions - android

I'm drawing a rectangle over a camera preview and i want to crop that specific region covered by the rectangle and display it in ImageView. I managed to crop the the Bitmap by using :
Bitmap croppedBmp = Bitmap.createBitmap(imageOriginal, 50, 100, 1000, 550);
Code to draw the rectangle :
Rect rect = new Rect(50, 100,1000, 550);
as you can see i'm using the same parameters but i'm not getting the desired result. What i'm missing here ?. Thank you in advance.
Camera Preview :
Cropped Bitmap displayed in new activity :

Related

Android: how to draw scaled bitmap on canvas?

my goal is to create an app that allows users to take a picture with the camera and then add some images on it just like "face in hole" but reversed.
I'm already capable of showing the camera preview while display a imageview, but when i take the picture it has a different resolution so the bitmap over the photo is misplaced.
I've wrote this code but it isn't very accurate:
canvas.drawBitmap(cameraBitmap, 0f, 0f, null); //drawing the picture just taken on the canvas
float posx, posy, newx,newy;
RectF r = new RectF();
i.getImageMatrix().mapRect(r); // i is the imageview of the bitmap
posx= r.left;
posy= r.top;
newx=(posx*newImage.getWidth())/screenWidth;
newy = (posy*newImage.getHeight())/screenHeight;
canvas.drawBitmap( ((BitmapDrawable)i.getDrawable()).getBitmap(),newx,newy,null);
My question is: there's a better and more accurate way to place a bitmap on the picture with the new resolution keeping bitmap scale and rotation info?
Thanks in advance.

How to cropping circle image openvc android

I want to crop a circle shape in an image.
I have an input image that is gray scale.
In this image have a circle shape. I need it.
How to do it using Open CV on Android?
Input image:
Bitmap bmpProces = BitmapFactory.decodeFile(path+inpuImage);
Mat imageMat = new Mat ( bmpProces.getHeight(), bmpProces.getWidth(), CvType.CV_8U);
Bitmap myBitmap32 = bmpProces.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, imageMat);
You have to create a mask,fill a circle in it according coordinates of target and use CopyTo to extract target circular area in your mask.Then you can crop a normal bounding rectangle of circle in mask Mat.You can see sample code and some more details here.There is a snippet code that you can convert it to java and use it:
// center and radius are the results of HoughCircle
// mask is a CV_8UC1 image with 0
cv::Mat mask = cv::Mat::zeros( img.rows, img.cols, CV_8UC1 );
circle( mask, center, radius, Scalar(255,255,255), -1, 8, 0 ); //-1 means filled
img.copyTo( dst, mask ); // copy values of img to dst if mask is > 0.

Android: Image is too big when using canvas drawBitmap

So here is what my app is doing:
I have a camera preview.
I have an overlay image on top of this camera preview.
When I capture the image I combine both the captured image and the overlay.
I display the combined imaged on an imageview.
However, the overlay is way too big when it's displayed on the imageview.
The overlay image is displayed perfectly when on the camera preview. It looks like the canvas.drawBitmap is scaling it somehow but I want to keep the original size.
IMPORTANT: This works fine on a Samsung S3, the issue occurs when using a Moto G.
Here's some of the code:
protected PictureCallback mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
cameraPreview.getCamera().stopPreview();
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix matrix = new Matrix();
matrix.preScale(-1, 1);
matrix.postRotate(90);
cameraBitmap = Bitmap.createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), matrix, true);
//Get the overlay image
ImageView imageview = (ImageView)findViewById(R.id.imageView2);
imageview.setDrawingCacheEnabled(true);
imageview.buildDrawingCache();
Bitmap overlayImage = Bitmap.createBitmap(imageview .getDrawingCache());
Bitmap mutableBitmap = cameraBitmap.copy(Bitmap.Config.ARGB_8888, true);
//Here is where I combine both images by drawing the overlay onto the captured bitmap
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawBitmap(overlayImage , 0 , canvas.getHeight() - overlayImage.getHeight(), paint);
//Here is where i set the image so that you can view the finished result - the combined image - but the overlay image is way too big!
ImageView capturedView = (ImageView)findViewById(R.id.capturedImageView);
capturedView.setImageBitmap(mutableBitmap);
Solved it! The solution was to forget about the canvas altogether and just get a screenshot of capturedView since it already had the captured image and the overlay on it.
Also, I had make sure to bring the overlay to front before getting the screenshot of capturedView.
// This is the imageview where I first display the
// image which was captured from the camera.
capturedView.bringToFront();
// This is the overlay imageview,
// by bringing this in front of the capturedView
// I can take a screenshot of it and get both the captured
// image and the overlay image.
imageview.bringToFront();
capturedView.setDrawingCacheEnabled(true);
capturedView.buildDrawingCache();
// And now you have a bitmap with both images combined :-)
combinedBitmap = capturedView.getDrawingCache();

drawBitmap is showing picture bigger than it is

Here's the sample of the code I've been using to show a part of an image on a canvas:
Bitmap map = BitmapFactory.decodeResource(getResources(), R.drawable.thecat);
//the cat is 600x600 single image that looks like a 3x3 table made of 200x200 images
Bitmap smallShaft = Bitmap.createBitmap(map, 200, 200, 200, 200);
//notice createBitmap(source, startX, startY, width, height)
Canvas c = holder.lockCanvas();
c.drawARGB(255, 124, 124, 255);
c.drawBitmap(smallShaft, 0,0, null);
holder.unlockCanvasAndPost(c);
And what I get is a 200x200 square that contains over-sized image. Despite I cut the 200x200 part off of an 600x600 image, it shows like I cut 200x200 part off of an (600*1.5)x(600*1.5) image.
What I get
Where I'm cutting from
One more detail you should know maybe.
It's been drawn on custom SurfaceView that implements Runnable and image is being redrawn in infinite loop.

Captured image stretches wrongly after cropping in Android

I want to capture an image from my camera and crop the captured image at specified co-ordinates then draw it on the middle of another image. The following code doesn't crash, but the captured image is screwing up, since the image stretches wrong.
Where am I going wrong?
Merry X'mas!
//Get the bottom image Bitmap
Bitmap bottomImage = BitmapFactory.decodeResource(getResources(), SelectDollarActivity.selectedImageId);
//Get the captured image Bitmap
Bitmap capturedImage = BitmapFactory.decodeFile(CaptureImage.cImagePath) ;
//************ CROP THE CAPTURED IMAGE *******************
int targetBitmapWidth = bottomImage.getWidth();
int targetBitmapHeight = bottomImage.getHeight() ;
//create a Bitmap with specified width & height
Bitmap clippedBitmap = Bitmap.createBitmap(targetBitmapWidth, targetBitmapHeight, Bitmap.Config.ARGB_8888);
//Construct a canvas with the specified bitmap to draw into.
Canvas canvas = new Canvas(clippedBitmap);
//************** cropping process goes HERE.........
//Create a new rectangle with the specified coordinates
RectF rectf = new RectF(left, top, right, bottom);
//Create an empty path
Path path = new Path();
//Add a closed oval contour to the path
path.addOval(rectf, Path.Direction.CW);
//Intersect the current clip with the specified path : CROPPING
canvas.clipPath(path);
canvas.drawBitmap(capturedImage, null, new Rect(0, 0, targetBitmapWidth, targetBitmapHeight), null);
//******** MERGING PROCESS *******************
//Construct a canvas with the specified bitmap to draw into.
Canvas combo = new Canvas(bottomImage);
// Then draw the second on top of that
combo.drawBitmap(clippedBitmap, 0f, 0f, null);
// bottomImage is now a composite of the two. so, display the bottom image
//************** DISPLAY THE MERGED IMAGE ****************
((ImageView)findViewById(R.id.billImage)).setImageBitmap(bottomImage);
Documentation states that drawBitmap accepts two more arguments, width and heights. In your code,
combo.drawBitmap(clippedBitmap, 0f, 0f, null);
only has the positioning.
You will need to set a few more arguments of course, but it should work :D

Categories

Resources