From 1f3570c11e1cdab7a45394832532e3ab1e3005d9 Mon Sep 17 00:00:00 2001 From: xiaowusky Date: Wed, 14 Jun 2023 10:47:56 +0800 Subject: [PATCH] =?UTF-8?q?desc:=E6=B0=B4=E5=8D=B0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yinuo/library/vlc/RtspSurfaceRender2.java | 5 +- .../java/com/yinuo/library/vlc/TxtOverlay.kt | 60 +++++++++++-------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/library-vlc/src/main/java/com/yinuo/library/vlc/RtspSurfaceRender2.java b/library-vlc/src/main/java/com/yinuo/library/vlc/RtspSurfaceRender2.java index e7c28f4..67f44c3 100644 --- a/library-vlc/src/main/java/com/yinuo/library/vlc/RtspSurfaceRender2.java +++ b/library-vlc/src/main/java/com/yinuo/library/vlc/RtspSurfaceRender2.java @@ -29,8 +29,6 @@ public class RtspSurfaceRender2 implements RtspHelper.RtspCallback { private final Object mBitmapLock = new Object(); private Bitmap mVideoBitmap = null; - private TxtOverlay mOverlay = null; - private boolean mIsResumed = false; private boolean mRecording = false; @@ -65,7 +63,6 @@ public class RtspSurfaceRender2 implements RtspHelper.RtspCallback { holder.addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { - mOverlay = new TxtOverlay(); } @Override @@ -118,7 +115,7 @@ public class RtspSurfaceRender2 implements RtspHelper.RtspCallback { @Override public void onPreviewFrame(final ByteBuffer buffer, int width, int height) { synchronized (mBitmapLock) { - Bitmap overLayBitmap = mOverlay.javaOverlayBm("1111111@2222222@333333"); + Bitmap overLayBitmap = TxtOverlay.INSTANCE.buildOverlayBitmap(); mVideoBitmap.copyPixelsFromBuffer(buffer.position(0)); mVideoBitmap = mergeBitmap(mVideoBitmap, overLayBitmap); buffer.clear(); diff --git a/library-vlc/src/main/java/com/yinuo/library/vlc/TxtOverlay.kt b/library-vlc/src/main/java/com/yinuo/library/vlc/TxtOverlay.kt index f3135ae..5fb62a9 100644 --- a/library-vlc/src/main/java/com/yinuo/library/vlc/TxtOverlay.kt +++ b/library-vlc/src/main/java/com/yinuo/library/vlc/TxtOverlay.kt @@ -1,44 +1,52 @@ -package com.yinuo.library.vlc; +package com.yinuo.library.vlc -import android.graphics.Bitmap; -import android.graphics.Color; -import android.text.TextUtils; - -import org.easydarwin.util.YUVUtils; - -import java.text.SimpleDateFormat; +import android.graphics.Bitmap +import android.graphics.Color +import android.text.TextUtils +import org.easydarwin.util.YUVUtils +import java.text.SimpleDateFormat /** * Created by John on 2017/2/23. */ +object TxtOverlay { + // 上一次展示的提示文字 + private var mLastShowTip = "" + + // 待展示的提示文字 + private var mToDoShowTip = "1111111@2222222@333333" -public class TxtOverlay { + // 外部调用,设置待显示水印文字 + fun setShowTip(string: String) { + mToDoShowTip = string + } - String mTip = ""; - long lastTipUpdateTime = 0; - Bitmap bmp; + // 上一次展示的时间 + private var lastTipUpdateTime: Long = 0 - SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); + // 文字生成的bitmap + private var bmp: Bitmap? = null - public TxtOverlay() { - } + // 时间格式化字符串 + private val dateFormat = SimpleDateFormat("yy-MM-dd HH:mm:ss") - public Bitmap javaOverlayBm(String txt) { - if (TextUtils.isEmpty(txt)) { - return null; + fun buildOverlayBitmap(): Bitmap? { + if (TextUtils.isEmpty(mToDoShowTip)) { + return null } - long currentTimeMillis = System.currentTimeMillis(); + val currentTimeMillis = System.currentTimeMillis() // 限制获取bitmap的频率,保证性能 - if (TextUtils.isEmpty(mTip) || (!txt.equals(mTip) || currentTimeMillis - lastTipUpdateTime > 1000)) { + if (TextUtils.isEmpty(mLastShowTip) || mToDoShowTip != mLastShowTip || currentTimeMillis - lastTipUpdateTime > 1000) { // 记录更新时间和上一次的文字 - lastTipUpdateTime = currentTimeMillis; - mTip = txt; + lastTipUpdateTime = currentTimeMillis + mLastShowTip = mToDoShowTip // 文字转bitmap - bmp = YUVUtils.generateBitmap(dateFormat.format(lastTipUpdateTime) + "@" + txt, 40, Color.WHITE); + bmp = YUVUtils.generateBitmap( + dateFormat.format(lastTipUpdateTime) + "@" + mToDoShowTip, 40, Color.WHITE + ) // 缩放旋转bitmap // bmp = YUVUtils.rotateImage(bmp, 0); } - return bmp; + return bmp } - -} +} \ No newline at end of file