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. 52
      app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.java

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

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

Loading…
Cancel
Save