|
@@ -8,7 +8,6 @@ import CommonEventManager from '@ohos.commonEventManager'
|
|
|
import uploadInstance from '../utils/UploadUtil';
|
|
|
import router from '@ohos.router';
|
|
|
import fileIo from '@ohos.file.fs';
|
|
|
-
|
|
|
const TAG = "openHarmonyBridge upload"
|
|
|
@Entry
|
|
|
@Component
|
|
@@ -203,60 +202,32 @@ struct Index {
|
|
|
readTimer = 0
|
|
|
|
|
|
liveShow = async () => {
|
|
|
- const context: Context = getContext(this);
|
|
|
+
|
|
|
+ const previewManager = new PreviewManager(getContext(this));
|
|
|
let index = 0;
|
|
|
- let lastImagePath = '';
|
|
|
|
|
|
- this.readTimer = setInterval( async () => {
|
|
|
- if (!this.isCloseView) {
|
|
|
+ this.readTimer = setInterval(async () => {
|
|
|
+ if (!this.isCloseView && !this.isCapturing) {
|
|
|
try {
|
|
|
- if(!this.isCapturing) {
|
|
|
- const filePath: string = `${context.filesDir}/live${index}.jpg`;
|
|
|
- // 检查文件是否存在
|
|
|
- try {
|
|
|
- fs.accessSync(filePath);
|
|
|
-
|
|
|
- // 只有当文件路径改变时才更新图像
|
|
|
- if (filePath !== lastImagePath) {
|
|
|
- const imageSource = image.createImageSource(filePath);
|
|
|
- if (imageSource) {
|
|
|
- try {
|
|
|
- // 创建新的PixelMap之后再释放旧的
|
|
|
- const newPixelMap = await imageSource.createPixelMap();
|
|
|
- if (this.commodityPixelMap) {
|
|
|
- this.commodityPixelMap.release();
|
|
|
- }
|
|
|
- this.commodityPixelMap = newPixelMap;
|
|
|
- imageSource.release();
|
|
|
- lastImagePath = filePath;
|
|
|
- index = (index + 1) % 10;
|
|
|
- } catch(error) {
|
|
|
- console.error(TAG, "创建PixelMap失败:", error);
|
|
|
- router.back()
|
|
|
- // break
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
- // await sleep(30);
|
|
|
- // continue;
|
|
|
- }
|
|
|
-
|
|
|
+ const success = await previewManager.processFrame(index);
|
|
|
+ if (success) {
|
|
|
+ this.commodityPixelMap = previewManager.getActiveBuffer();
|
|
|
+ //index = (index + 1) % 10;
|
|
|
}
|
|
|
- // 减小循环间隔提高流畅度
|
|
|
-
|
|
|
} catch (error) {
|
|
|
- console.error(TAG, "预览错误:", error);
|
|
|
-
|
|
|
+ console.warn(TAG, "预览更新失败:", error);
|
|
|
}
|
|
|
}
|
|
|
- }, 50)
|
|
|
-
|
|
|
- // 退出时清理资源
|
|
|
- if(this.commodityPixelMap) {
|
|
|
- this.commodityPixelMap.release();
|
|
|
- this.commodityPixelMap = null;
|
|
|
- }
|
|
|
+ }, 50); // 30ms interval for ~30fps
|
|
|
+
|
|
|
+ // 清理资源
|
|
|
+ return () => {
|
|
|
+ previewManager.release();
|
|
|
+ if (this.commodityPixelMap) {
|
|
|
+ this.commodityPixelMap.release();
|
|
|
+ this.commodityPixelMap = null;
|
|
|
+ }
|
|
|
+ };
|
|
|
};
|
|
|
|
|
|
aboutToAppear(): void {
|
|
@@ -272,10 +243,14 @@ struct Index {
|
|
|
if (this.subscriber) {
|
|
|
CommonEventManager.unsubscribe(this.subscriber);
|
|
|
}
|
|
|
-
|
|
|
- clearInterval(this.readTimer)
|
|
|
+ if (this.readTimer) {
|
|
|
+ clearInterval(this.readTimer);
|
|
|
+ }
|
|
|
+ if (this.commodityPixelMap) {
|
|
|
+ this.commodityPixelMap.release();
|
|
|
+ this.commodityPixelMap = null;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
build() {
|
|
|
Column() {
|
|
|
Row() {
|
|
@@ -465,4 +440,81 @@ function delayExecution(callback: Function, delay: number) {
|
|
|
clearInterval(timerId);
|
|
|
callback();
|
|
|
}, delay);
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+class PreviewManager {
|
|
|
+ private context: Context;
|
|
|
+ private activeBuffer: PixelMap | null = null;
|
|
|
+ private nextBuffer: PixelMap | null = null;
|
|
|
+ private isProcessing: boolean = false;
|
|
|
+ private lastFrameTime: number = 0;
|
|
|
+ private frameInterval: number = 50;
|
|
|
+
|
|
|
+ constructor(context: Context) {
|
|
|
+ this.context = context;
|
|
|
+ }
|
|
|
+
|
|
|
+ async processFrame(index: number): Promise<boolean> {
|
|
|
+ if (this.isProcessing) return false;
|
|
|
+
|
|
|
+ const now = Date.now();
|
|
|
+ if (now - this.lastFrameTime < this.frameInterval) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.isProcessing = true;
|
|
|
+ try {
|
|
|
+ const filePath = `${this.context.filesDir}/live${index}.jpg`;
|
|
|
+
|
|
|
+ // 检查文件是否存在
|
|
|
+ try {
|
|
|
+ fs.accessSync(filePath);
|
|
|
+ } catch {
|
|
|
+ this.isProcessing = false;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ const imageSource = image.createImageSource(filePath);
|
|
|
+ if (!imageSource) {
|
|
|
+ this.isProcessing = false;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建较小尺寸的PixelMap以提高性能
|
|
|
+
|
|
|
+ this.nextBuffer = await imageSource.createPixelMap();
|
|
|
+ imageSource.release();
|
|
|
+
|
|
|
+ // 交换缓冲区
|
|
|
+ if (this.activeBuffer) {
|
|
|
+ this.activeBuffer.release();
|
|
|
+ }
|
|
|
+ this.activeBuffer = this.nextBuffer;
|
|
|
+ this.nextBuffer = null;
|
|
|
+ this.lastFrameTime = now;
|
|
|
+ return true;
|
|
|
+ } catch (error) {
|
|
|
+ console.warn(TAG, "处理预览帧失败:", error);
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ this.isProcessing = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ getActiveBuffer(): PixelMap | null {
|
|
|
+ return this.activeBuffer;
|
|
|
+ }
|
|
|
+
|
|
|
+ release() {
|
|
|
+ if (this.activeBuffer) {
|
|
|
+ this.activeBuffer.release();
|
|
|
+ this.activeBuffer = null;
|
|
|
+ }
|
|
|
+ if (this.nextBuffer) {
|
|
|
+ this.nextBuffer.release();
|
|
|
+ this.nextBuffer = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|