在 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”。因此,您需要明确指定一个。
这里是a link to the Android font resources reference,
the first example in FFmpeg drawtext
documentation 说明了如何指定字体文件。
(我不是 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();
}
我希望这个解决方案对其他人有用。
大家好。 我正在尝试使用此 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”。因此,您需要明确指定一个。
这里是a link to the Android font resources reference,
the first example in FFmpeg drawtext
documentation 说明了如何指定字体文件。
(我不是 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();
}
我希望这个解决方案对其他人有用。