convert text to transparent bitmap

convert text to transparent bitmap
pull/166/head
xufulong 5 years ago
parent 5002560f11
commit a73c50693a
  1. 2
      app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java
  2. 50
      app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.java

@ -182,7 +182,7 @@ public class VideoHandleActivity extends BaseActivity {
case TYPE_TEXT:// text
String text = "Hello,FFmpeg";
String textPath = PATH + File.separator + "text.jpg";
boolean result = BitmapUtil.textToPicture(textPath, text, this);
boolean result = BitmapUtil.textToPicture(textPath, text);
Log.i(TAG, "text to picture result=" + result);
String textMark = PATH + File.separator + "textMark.mp4";
commandLine = FFmpegUtil.addWaterMarkImg(srcFile, textPath, location, bitRate, offsetXY, textMark);

@ -2,11 +2,9 @@ package com.frank.ffmpeg.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.graphics.Paint;
import java.io.File;
import java.io.FileOutputStream;
@ -26,29 +24,24 @@ public class BitmapUtil {
* convert text to bitmap
*
* @param text text
* @param context context
* @return bitmap of teh text
*/
private static Bitmap textToBitmap(String text, Context context) {
float scale = context.getResources().getDisplayMetrics().scaledDensity;
TextView tv = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(layoutParams);
tv.setText(text);
tv.setTextSize(scale * TEXT_SIZE);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setDrawingCacheEnabled(true);
tv.setTextColor(TEXT_COLOR);
tv.setBackgroundColor(Color.WHITE);
tv.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
tv.buildDrawingCache();
Bitmap bitmap = tv.getDrawingCache();
int rate = bitmap.getHeight() / 20;
return Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / rate, 20, false);
private static Bitmap textToBitmap(String text) {
Paint paint = new Paint();
paint.setTextSize(TEXT_SIZE);
paint.setTextAlign(Paint.Align.LEFT);
paint.setColor(TEXT_COLOR);
paint.setDither(true);
paint.setAntiAlias(true);
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
int width = (int)paint.measureText(text);
int height = fm.descent - fm.ascent;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, 0, fm.leading - fm.ascent, paint);
canvas.save();
return bitmap;
}
/**
@ -56,15 +49,14 @@ public class BitmapUtil {
*
* @param filePath filePath
* @param text text
* @param context context
* @return result of generating picture
*/
public static boolean textToPicture(String filePath, String text, Context context) {
Bitmap bitmap = textToBitmap(text, context);
public static boolean textToPicture(String filePath, String text) {
Bitmap bitmap = textToBitmap(text);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();

Loading…
Cancel
Save