I'm using google APIs for face detection in android. When a face is detected I'm drawing an image on the canvas. I'm trying to capture the image with the overlay by clicking a button. In the method
public void onPictureTaken(byte[] bytes)
, the bytes contain only the image without the overlay drawn on it. Is there a way to save the image with the overlay on it?
Asked
Active
Viewed 1,396 times
2

user6120360
- 21
- 3
-
I get the overlay from the class GraphicOverlay with `this.getDrawingCache();` , but it always returns the same overlay. For example, when I change the overlay it always return the first overlay. Plus when the image is captured, it is rotated, sometimes for 90, sometimes for -90 degrees. The overlay I get is in the right position, so when I put it over the image it doesn't look right. – user6120360 Mar 27 '16 at 11:49
-
1Keep in mind that there are three different coordinate systems that you need to manage: (1) preview image coordinates - these are the coordinates that the face detector reported, (2) drawing canvas coordinates - these are the coordinates used for drawing the overlay, (3) picture image coordinates - differs from preview in that it is probably higher resolution. Note also that if you are using the front facing camera, the preview/overlay will be flipped horizontally (a mirror image). – pm0733464 Mar 28 '16 at 14:23
-
how do I get the right overlay when captured button is clicked? – user6120360 Mar 29 '16 at 20:55
-
If you have solved this problem please post the solution. I want to complete the same task. – hardik9850 Jun 07 '16 at 08:46
-
I haven't found a solution yet. But I'll start working on it soon when I'll have more free time. If I find any kind of solution I will post it here. – user6120360 Jun 07 '16 at 19:55
-
@user6120360 did u figure out anything? – Muhammad Umar Sep 12 '17 at 10:01
1 Answers
0
You have to
create a new canvas backed by a new bitmap,
Bitmap picture = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Bitmap resizedBitmap = Bitmap.createBitmap(mGraphicOverlay.getWidth(),mGraphicOverlay.getHeight(),picture.getConfig()); Canvas canvas = new Canvas(resizedBitmap);
draw the captured bitmap over it
- draw the overlays over it
- save new bitmap
Also in order for the overlays to match the captured picture you either need to mirror the picture or the overlay
Matrix matrix = new Matrix();
matrix.setScale((float)resizedBitmap.getWidth()/(float)picture.getWidth(),(float)resizedBitmap.getHeight()/(float)picture.getHeight());
// mirror by inverting scale and translating
matrix.preScale(-1, 1);
matrix.postTranslate(canvas.getWidth(), 0);
Paint paint = new Paint();
canvas.drawBitmap(picture,matrix,paint);
tracker.getmEyesGraphic().draw(canvas); // make those accessible

Fabio
- 2,654
- 16
- 31