|
@@ -7,6 +7,7 @@ import commonEvent from "@ohos.commonEvent";
|
|
|
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
|
|
@@ -19,8 +20,16 @@ struct Index {
|
|
|
* */
|
|
|
@State commodityPixelMap: PixelMap | null = null;
|
|
|
@State isButtonPressed: boolean = false;
|
|
|
- @State isCapturing: boolean = false; // 新增状态控制拍摄状态
|
|
|
-//
|
|
|
+ @State isCapturing: boolean = false;
|
|
|
+ @State isCloseView: boolean = false;// 新增状态控制拍摄状态
|
|
|
+ @State reminds:string = ''
|
|
|
+
|
|
|
+ remindController: CustomDialogController = new CustomDialogController({
|
|
|
+ builder: RemindDialog({
|
|
|
+ remind: this.reminds,}
|
|
|
+ ),
|
|
|
+ })
|
|
|
+ //
|
|
|
subscriber: CommonEventManager.CommonEventSubscriber | null = null; // Used to save the created subscriber object for subsequent subscription and unsubscription.
|
|
|
// Subscriber information.
|
|
|
subscribeInfo: CommonEventManager.CommonEventSubscribeInfo = { events: ["sonycamera_callback"] };
|
|
@@ -43,14 +52,18 @@ struct Index {
|
|
|
createSubscriber = async () => {
|
|
|
this.subscriber = await CommonEventManager.createSubscriber(this.subscribeInfo);
|
|
|
if (this.subscriber) {
|
|
|
- console.info("[CommonEvent] Subscriber created successfully");
|
|
|
+ console.info(TAG,"创建订阅回调成功");
|
|
|
CommonEventManager.subscribe(this.subscriber, (err, data) => {
|
|
|
if (err?.code) {
|
|
|
console.error("[CommonEvent] SubscribeCallBack err=" + JSON.stringify(err));
|
|
|
} else {
|
|
|
console.info("[CommonEvent] SubscribeCallBack data=" + JSON.stringify(data));
|
|
|
if (data && data.code&&data.code === 2) {
|
|
|
+ console.info(TAG,"开始传输照片");
|
|
|
this.handleCaptureSuccess()
|
|
|
+ }else if (data && data.code&&data.code === 3) {
|
|
|
+ console.info(TAG,"开始预览" );
|
|
|
+ this.publishStartView();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
@@ -59,33 +72,40 @@ struct Index {
|
|
|
|
|
|
handleCaptureSuccess = () => {
|
|
|
this.savePhoto();
|
|
|
+ // commonEvent.publish("startview", (err) => {
|
|
|
+ // if (err?.code) {
|
|
|
+ // console.error("Restart preview error:", JSON.stringify(err));
|
|
|
+ // } else {
|
|
|
+ // this.isCapturing = false;
|
|
|
+ // this.isCloseView =false;
|
|
|
+ // this.liveShow();// 拍摄完成后恢复预览
|
|
|
+ // console.log("Restart success")
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ };
|
|
|
+ publishStartView=()=>{
|
|
|
commonEvent.publish("startview", (err) => {
|
|
|
if (err?.code) {
|
|
|
console.error("Restart preview error:", JSON.stringify(err));
|
|
|
} else {
|
|
|
- this.isCapturing = false; // 拍摄完成后恢复预览
|
|
|
+ this.isCapturing = false;
|
|
|
+ this.isCloseView =false;
|
|
|
+ this.liveShow();// 拍摄完成后恢复预览
|
|
|
console.log("Restart success")
|
|
|
}
|
|
|
});
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
connectCamera = async () => {
|
|
|
commonEvent.publish("opensession", (err) => {
|
|
|
if (err?.code) {
|
|
|
console.info("[CommonEvent] Publish CallBack err=" + JSON.stringify(err))
|
|
|
} else {
|
|
|
- commonEvent.publish("startview", (err) => {
|
|
|
- console.info("[CommonEvent] Publish opensession succeed ")
|
|
|
- if (err?.code) {
|
|
|
- console.info("[CommonEvent] Publish CallBack err=" + JSON.stringify(err))
|
|
|
- } else {
|
|
|
- console.info("[CommonEvent] Publish startview succeed ")
|
|
|
- }
|
|
|
- })
|
|
|
+ console.info("[CommonEvent] Publish opensession succeed ")
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
- disconnectCamera = async () => {
|
|
|
+ disconnectCamera = () => {
|
|
|
commonEvent.publish("stopview", (err) => {
|
|
|
if (err?.code) {
|
|
|
console.info("[CommonEvent] Publish CallBack err=" + JSON.stringify(err))
|
|
@@ -101,12 +121,28 @@ struct Index {
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
- savePhoto=async()=>{
|
|
|
- const context = getContext() as common.UIAbilityContext;
|
|
|
+ savePhoto=()=>{
|
|
|
+ let imageUri: string = "/data/storage/el2/base/haps/entry/files/image_base64.txt"
|
|
|
console.log(TAG, "进入了savephoto")
|
|
|
try {
|
|
|
+ const file = fileIo.openSync(imageUri, fileIo.OpenMode.READ_ONLY);
|
|
|
+ fileIo.closeSync(file.fd); // 关闭文件描述符
|
|
|
+ this.reminds= "照片传输到小黑盒成功"
|
|
|
+ this.remindController.open()
|
|
|
+ delayExecution(()=>{
|
|
|
+ this.remindController.close()
|
|
|
+ }, 1000)
|
|
|
+ } catch (error) {
|
|
|
+ console.error('文件不存在或无法访问:', error);
|
|
|
+ this.reminds= "照片传输到小黑盒失败"
|
|
|
+ this.remindController.open()
|
|
|
+ delayExecution(()=>{
|
|
|
+ this.remindController.close()
|
|
|
+ }, 1000)
|
|
|
+
|
|
|
+ }
|
|
|
+ try {
|
|
|
// 获取相册访问的 helper 实例
|
|
|
- const helper = photoAccessHelper.getPhotoAccessHelper(context);
|
|
|
|
|
|
console.log(TAG, "进入了savephoto1")
|
|
|
// 创建一个新的图片资产,并获取目标 URI
|
|
@@ -114,13 +150,14 @@ struct Index {
|
|
|
|
|
|
// console.log(TAG, "进入了savephoto2")
|
|
|
// 打开源文件和目标文件 这个是写死的 app的名字不能改
|
|
|
- let imageUri: string = "/data/storage/el2/base/haps/entry/files/shoots.jpg"
|
|
|
+ // let imageUri: string = "/data/storage/el2/base/haps/entry/files/shoots.jpg"
|
|
|
// 开始上传
|
|
|
console.log(TAG, "开始上传", imageUri)
|
|
|
- uploadInstance.startUpload(imageUri, () => {
|
|
|
+ uploadInstance.startUploadBase64(imageUri, () => {
|
|
|
|
|
|
console.log(TAG, "上传完成后的callback")
|
|
|
this.isUploading = false
|
|
|
+ this.publishStartView();
|
|
|
})
|
|
|
|
|
|
|
|
@@ -143,6 +180,7 @@ struct Index {
|
|
|
}
|
|
|
}
|
|
|
takePhoto = async () => {
|
|
|
+ this.isCloseView = true;
|
|
|
this.isCapturing = true;
|
|
|
commonEvent.publish("stopview", (err) => {
|
|
|
if (err?.code) {
|
|
@@ -161,27 +199,64 @@ struct Index {
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
+
|
|
|
+ readTimer = 0
|
|
|
+
|
|
|
liveShow = async () => {
|
|
|
const context: Context = getContext(this);
|
|
|
let index = 0;
|
|
|
- while (true) {
|
|
|
- if(!this.isCapturing) {
|
|
|
- const filePath: string = `${context.filesDir}/live${index}.jpg`;
|
|
|
- console.info("icamera"+filePath)
|
|
|
+ let lastImagePath = '';
|
|
|
+
|
|
|
+ this.readTimer = setInterval( async () => {
|
|
|
+ if (!this.isCloseView) {
|
|
|
try {
|
|
|
- const imageSource = image.createImageSource(filePath);
|
|
|
- if (imageSource) {
|
|
|
- this.commodityPixelMap = await imageSource.createPixelMap();
|
|
|
+ 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);
|
|
|
+ // break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ // await sleep(30);
|
|
|
+ // continue;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
- console.info("demo"+"file exists");
|
|
|
+ // 减小循环间隔提高流畅度
|
|
|
+
|
|
|
} catch (error) {
|
|
|
- console.info("demo"+"file not exists");
|
|
|
+ console.error(TAG, "预览错误:", error);
|
|
|
+
|
|
|
}
|
|
|
- index = (index + 1) % 10;
|
|
|
}
|
|
|
- await sleep(50);
|
|
|
+ }, 50)
|
|
|
+
|
|
|
+ // 退出时清理资源
|
|
|
+ if(this.commodityPixelMap) {
|
|
|
+ this.commodityPixelMap.release();
|
|
|
+ this.commodityPixelMap = null;
|
|
|
}
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
aboutToAppear(): void {
|
|
|
this.createSubscriber();
|
|
@@ -191,10 +266,13 @@ struct Index {
|
|
|
}
|
|
|
|
|
|
aboutToDisappear(): void {
|
|
|
+ this.isCloseView = true;
|
|
|
this.disconnectCamera();
|
|
|
if (this.subscriber) {
|
|
|
CommonEventManager.unsubscribe(this.subscriber);
|
|
|
}
|
|
|
+
|
|
|
+ clearInterval(this.readTimer)
|
|
|
}
|
|
|
|
|
|
build() {
|
|
@@ -213,21 +291,30 @@ struct Index {
|
|
|
.height('100%')
|
|
|
.objectFit(ImageFit.Contain)
|
|
|
|
|
|
- if (this.isUploading) {
|
|
|
- Column() {
|
|
|
- Text('正在上传中...').fontSize(22).margin({ top: 10, bottom: 10 })
|
|
|
- LoadingProgress()
|
|
|
- .color(Color.Blue)
|
|
|
- .layoutWeight(1)
|
|
|
- }
|
|
|
- .height(140)
|
|
|
- .width(150)
|
|
|
- .backgroundColor(Color.White)
|
|
|
- .borderRadius(10)
|
|
|
- }
|
|
|
-
|
|
|
}
|
|
|
+ }
|
|
|
+ if (this.isUploading) {
|
|
|
+ Stack({ alignContent: Alignment.Center }) {
|
|
|
+ // 半透明黑色背景蒙层
|
|
|
+ Column() {
|
|
|
+ Text('正在上传中...')
|
|
|
+ .fontSize(22)
|
|
|
+ .margin({ top: 10, bottom: 10 })
|
|
|
|
|
|
+ LoadingProgress()
|
|
|
+ .color(Color.Blue)
|
|
|
+ .width(50)
|
|
|
+ .height(50)
|
|
|
+ }
|
|
|
+ .height(140)
|
|
|
+ .width(150)
|
|
|
+ .backgroundColor(Color.White)
|
|
|
+ .borderRadius(10)
|
|
|
+ .justifyContent(FlexAlign.Center)
|
|
|
+ }
|
|
|
+ .width('100%')
|
|
|
+ .height('100%')
|
|
|
+ .position({ x: 0, y: 0 })
|
|
|
}
|
|
|
}
|
|
|
.width('100%')
|
|
@@ -269,6 +356,9 @@ struct Index {
|
|
|
.height(70)
|
|
|
.backgroundColor('#1c1c1c')
|
|
|
.onClick(() => {
|
|
|
+ this.isCloseView = true;
|
|
|
+ sleep(100)
|
|
|
+ this.disconnectCamera()
|
|
|
router.back() })
|
|
|
|
|
|
}.width('100%')
|
|
@@ -356,5 +446,22 @@ struct Index {
|
|
|
function sleep(ms: number): Promise<void> {
|
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
}
|
|
|
+@CustomDialog
|
|
|
+struct RemindDialog {
|
|
|
+ controller: CustomDialogController
|
|
|
+ @Link remind: string
|
|
|
+ build() {
|
|
|
+ Column() {
|
|
|
+ Text(this.remind)
|
|
|
+ .fontSize(20)
|
|
|
+ .margin({ top: 10, bottom: 10 })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
+function delayExecution(callback: Function, delay: number) {
|
|
|
+ let timerId = setInterval(() => {
|
|
|
+ clearInterval(timerId);
|
|
|
+ callback();
|
|
|
+ }, delay);
|
|
|
+}
|