拍摄后在图像上叠加徽标和细节

问题描述 投票:0回答:1

我正在尝试实现类似于下图的结果。我想在图像的右上角显示文件信息地址徽标。我开发了一个应用程序,允许用户拍照并添加文件名地址和徽标,但结果不是我所期望的。文字在拍摄的照片上占用了太多空间。

如果您有一个解决方案来实现类似于所需照片的结果,我将不胜感激。

谢谢, 尼古拉斯

我的应用程序的输出

Output from my Application

Output I would like to get

预期结果

我正在 Android Studio 上使用 opencv 库来归档我尝试此代码的请求

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

拍摄的图片代码:

 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);
    }
}
java android graphics drawing
1个回答
0
投票

我尝试更新代码,但是覆盖位图的文本看起来不像预期的结果,并且如果文本太长,文本将变得完全不可读。 :

public class ImageTextOverlay {

public static Bitmap drawTextOnBitmap(Bitmap originalBitmap, String address, String fileName) {
    Bitmap bitmapWithText = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);

    Canvas canvas = new Canvas(bitmapWithText);

    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);

    int textSize = (int) (originalBitmap.getHeight() * 0.05);
    paint.setTextSize(textSize / 2);
    int margin = 20 - Math.round(paint.getTextSize());

    String date = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault()).format(new Date());
    String text = fileName + " Adresse: " + address + " Date: " + date;

    Rect textBounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), textBounds);

    // Positionner le texte en bas de l'image
    int xPos = margin;
    int yPos = originalBitmap.getHeight() - margin;

    canvas.drawText(text, xPos, yPos, paint);

    return bitmapWithText;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.