簡體   English   中英

在 Android 上使用 FFMPEG 庫在圖像上放置文字

[英]Text over Image using FFMPEG library on Android

美好的一天,伙計們。 我正在嘗試使用此 ffmpeg 命令在圖像上添加文本

String exe = " -i /storage/emulated/0/Download/test1.jpg -vf drawtext=text='Test Text':fontcolor=white:fontsize=75:x=1002:y=100: " + file.getAbsolutePath();

但不幸的是我遇到了這個錯誤,

Input #0, image2, from '/storage/emulated/0/Download/test1.jpg':
Duration: 00:00:00.04, start: 0.000000, bitrate: 8264 kb/s
Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 960x1280, 25 fps, 25 tbr, 25 tbn
Stream mapping:
Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[Parsed_drawtext_0 @ 0xa38921b0] Cannot find a valid font for the family Sans
[AVFilterGraph @ 0xedd90500] Error initializing filter 'drawtext'[AVFilterGraph @ 0xedd90500]  with args 'text=Test Text:fontcolor=white:fontsize=75:x=1002:y=100:'[AVFilterGraph @ 0xedd90500] 
Error reinitializing filters!
Failed to inject frame into filter network: No such file or directory
Error while processing the decoded data for stream #0:0
Conversion failed!

有人有和我一樣的錯誤嗎? 謝謝!

這是您需要關注的錯誤消息(FFmpeg 的錯誤日志有時會產生誤導):

[Parsed_drawtext_0 @ 0xa38921b0] Cannot find a valid font for the family Sans

發生這種情況是因為您沒有指定字體,並且在您的系統上找不到默認字體“Sans”。 因此,您需要明確指定一個。

這是Android 字體資源參考的鏈接,FFmpeg drawtext文檔中的第一個示例說明了如何指定字體文件。

(我不是 Android 開發人員,所以希望您可以從這些鏈接中找出答案。)

我在 Android 上使用 Canvas 而不是使用 ffmpeg 庫找到了一個非常簡單和快速的解決方案。

這是我的代碼解決方案:

    ImageView mImageView = view.findViewById(R.id.imgView1);
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);

    Bitmap.Config config = bm.getConfig();
    int width = bm.getWidth();
    int height = bm.getHeight();

    Bitmap newImage = Bitmap.createBitmap(width, height, config);

    Canvas c = new Canvas(newImage);
    c.drawBitmap(bm, 0, 0, null);

    Paint paint = new Paint();
    paint.setColor(Color.YELLOW);
    paint.setStyle(Paint.Style.FILL);
    paint.setTextSize(150);

    c.drawText("Testare hai noroc",  bm.getWidth()/3, bm.getHeight()/2, paint);

    mImageView.setImageBitmap(newImage);

    File file = new File("/storage/emulated/0/Download/",System.currentTimeMillis() + ".jpeg");

    try {
        newImage.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
    } catch (Exception e) {
        e.printStackTrace();
    } 

我希望這個解決方案對其他人有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM