Take picture with drawable/paint on face using vision api

后端 未结 3 1120
心在旅途
心在旅途 2021-02-05 12:29

What I am trying?

I am trying to take picture with drawable/paint on face but, i am not able to get both on same picture.

What

3条回答
  •  失恋的感觉
    2021-02-05 13:11

    You are very close to achieve what you need :)

    You have:

    1. An image from the Camera of the face (First code snippet)
    2. An image from the Canvas of the eyes overlay (Second code snippet)

    What you need:

    • An image that has the face with the eyes overlay on top - A merged image.

    How to merge?

    To merge 2 images simply use a canvas, like so:

    public Bitmap mergeBitmaps(Bitmap face, Bitmap overlay) {
        // Create a new image with target size
        int width = face.getWidth();
        int height = face.getHeight();
        Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    
        Rect faceRect = new Rect(0,0,width,height);
        Rect overlayRect = new Rect(0,0,overlay.getWidth(),overlay.getHeight());
    
        // Draw face and then overlay (Make sure rects are as needed)
        Canvas canvas = new Canvas(newBitmap); 
        canvas.drawBitmap(face, faceRect, faceRect, null);
        canvas.drawBitmap(overlay, overlayRect, faceRect, null);
        return newBitmap
    }
    

    Then you can save the new image, as you are doing now.

    Full code would look like:

    mCameraSource.takePicture(shutterCallback, new 
    CameraSource.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] bytes) {
                // Generate the Face Bitmap
                BitmapFactory.Options options = new BitmapFactory.Options();
                Bitmap face = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
    
                // Generate the Eyes Overlay Bitmap
                mPreview.setDrawingCacheEnabled(true);
                Bitmap overlay = mPreview.getDrawingCache();
    
                // Generate the final merged image
                Bitmap result = mergeBitmaps(face, overlay);
    
                // Save result image to file
                try {
                    String mainpath = getExternalStorageDirectory() + separator + "TestXyz" + separator + "images" + separator;
                    File basePath = new File(mainpath);
                    if (!basePath.exists())
                        Log.d("CAPTURE_BASE_PATH", basePath.mkdirs() ? "Success": "Failed");
                    String path = mainpath + "photo_" + getPhotoTime() + ".jpg";
                    File captureFile = new File(path);
                    captureFile.createNewFile();
                    if (!captureFile.exists())
                        Log.d("CAPTURE_FILE_PATH", captureFile.createNewFile() ? "Success": "Failed");
                    FileOutputStream stream = new FileOutputStream(captureFile);
                    result.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    stream.flush();
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    

    Note that the above is just an example code. You should probably move the merging and saving to a file to a background thread.

提交回复
热议问题