From 1a73f8e0275b715e40f58ca67098b8f8863c6f3a Mon Sep 17 00:00:00 2001 From: xufuji456 <839789740@qq.com> Date: Sat, 30 Jan 2021 22:45:12 +0800 Subject: [PATCH] change BitmapUtil to kotlin --- .../com/frank/ffmpeg/util/BitmapUtil.java | 89 ------------------- .../java/com/frank/ffmpeg/util/BitmapUtil.kt | 88 ++++++++++++++++++ 2 files changed, 88 insertions(+), 89 deletions(-) delete mode 100644 app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.java create mode 100644 app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.kt diff --git a/app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.java b/app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.java deleted file mode 100644 index 6432dc6..0000000 --- a/app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.frank.ffmpeg.util; - -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.text.TextUtils; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; - -/** - * bitmap tool - * Created by frank on 2018/1/24. - */ - -public class BitmapUtil { - - /** - * convert text to bitmap - * - * @param text text - * @return bitmap of teh text - */ - private static Bitmap textToBitmap(String text, int textColor, int textSize) { - if (TextUtils.isEmpty(text) || textSize <= 0) { - return null; - } - Paint paint = new Paint(); - paint.setTextSize(textSize); - paint.setTextAlign(Paint.Align.LEFT); - paint.setColor(textColor); - 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; - } - - /** - * convert text to picture - * - * @param filePath filePath - * @param text text - * @return result of generating picture - */ - public static boolean textToPicture(String filePath, String text, int textColor, int textSize) { - Bitmap bitmap = textToBitmap(text, textColor, textSize); - if (bitmap == null || TextUtils.isEmpty(filePath)) { - return false; - } - FileOutputStream outputStream = null; - try { - outputStream = new FileOutputStream(filePath); - bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); - outputStream.flush(); - } catch (IOException e) { - e.printStackTrace(); - return false; - } finally { - try { - if (outputStream != null) { - outputStream.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - return true; - } - - /** - * delete file - * - * @param filePath filePath - * @return result of deletion - */ - public static boolean deleteTextFile(String filePath) { - File file = new File(filePath); - return file.exists() && file.delete(); - } - -} diff --git a/app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.kt b/app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.kt new file mode 100644 index 0000000..cd8d0ac --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/util/BitmapUtil.kt @@ -0,0 +1,88 @@ +package com.frank.ffmpeg.util + +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Paint +import android.text.TextUtils + +import java.io.File +import java.io.FileOutputStream +import java.io.IOException + +/** + * bitmap tool + * Created by frank on 2018/1/24. + */ + +object BitmapUtil { + + /** + * convert text to bitmap + * + * @param text text + * @return bitmap of teh text + */ + private fun textToBitmap(text: String, textColor: Int, textSize: Int): Bitmap? { + if (TextUtils.isEmpty(text) || textSize <= 0) { + return null + } + val paint = Paint() + paint.textSize = textSize.toFloat() + paint.textAlign = Paint.Align.LEFT + paint.color = textColor + paint.isDither = true + paint.isAntiAlias = true + val fm = paint.fontMetricsInt + val width = paint.measureText(text).toInt() + val height = fm.descent - fm.ascent + + val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) + canvas.drawText(text, 0f, (fm.leading - fm.ascent).toFloat(), paint) + canvas.save() + return bitmap + } + + /** + * convert text to picture + * + * @param filePath filePath + * @param text text + * @return result of generating picture + */ + fun textToPicture(filePath: String, text: String, textColor: Int, textSize: Int): Boolean { + val bitmap = textToBitmap(text, textColor, textSize) + if (bitmap == null || TextUtils.isEmpty(filePath)) { + return false + } + var outputStream: FileOutputStream? = null + try { + outputStream = FileOutputStream(filePath) + bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream) + outputStream.flush() + } catch (e: IOException) { + e.printStackTrace() + return false + } finally { + try { + outputStream?.close() + } catch (e: IOException) { + e.printStackTrace() + } + + } + return true + } + + /** + * delete file + * + * @param filePath filePath + * @return result of deletion + */ + fun deleteTextFile(filePath: String): Boolean { + val file = File(filePath) + return file.exists() && file.delete() + } + +}