OiO.lk Blog java Overlay a Logo and Details on an Image After Capture
java

Overlay a Logo and Details on an Image After Capture


I am trying to achieve a result similar to the photo below. I want to display the file information, address, and logo in the top right corner of the image. I have developed an application that allows users to take a photo and add the file name, address, and a logo to it, but the result is not what I expected. The text takes up too much space on the captured photo.

If you have a solution to achieve a result similar to the desired photo, I would greatly appreciate it.

Thanks,
Nicolas

Output from my application

Expected result

I’m using opencv library on Android Studio to archieve the request I tried this code to

public static Bitmap addTextToImage(Bitmap bitmap, String filename, String address) {
    Mat mat = new Mat();
    Utils.bitmapToMat(bitmap, mat);

    Scalar textColor = new Scalar(120,156,181); 

    int fontFace = Imgproc.FONT_HERSHEY_SIMPLEX;
    double fontScale = 0.21; 
    int thickness = 1;
    int padding = 5; 
    
    String text = filename + " Adresse: " + address;

    Size textSize = Imgproc.getTextSize(text, fontFace, fontScale, thickness, new int[1]);

    // Position du texte : en bas à droite
    Point textPosition = new Point(
            mat.cols() - textSize.width - padding,
            mat.rows() - padding
    );

    // Dessiner un rectangle semi-transparent derrière le texte
    Rect textBackground = new Rect(
            (int) (textPosition.x - padding),
            (int) (textPosition.y - textSize.height - padding),
            (int) (textSize.width + padding * 2),
            (int) (textSize.height + padding)
    );

    Scalar backgroundColor = new Scalar(255, 255, 255, 150); 
    Imgproc.rectangle(mat, textBackground.tl(), textBackground.br(), backgroundColor, Imgproc.FILLED);
    
    Imgproc.putText(mat, text, textPosition, fontFace, fontScale, textColor, thickness);

    // Convertir à nouveau Mat en Bitmap
    Bitmap resultBitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(mat, resultBitmap);

    return resultBitmap;
}

Picture code taken:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Camera.REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

        Location lastLoc = loc.getLastLoc();

        if(lastLoc == null)
        {
            return;
        }

        String filename = generateFileName();
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        Bitmap finalBitmap = PhotoAnnotator.addTextToImage(imageBitmap, filename, mSelectedAddress);

        saveImageToGallery(filename, finalBitmap, lastLoc);
    }
}



You need to sign in to view this answers

Exit mobile version