Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion harmony/camera_kit/src/main/ets/common/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class Constants {
static readonly X_COMPONENT_SURFACE_HEIGHT = 1080;
static readonly PERMISSION_LIST: Array<Permissions> =
[
"ohos.permission.CAMERA", "ohos.permission.MICROPHONE", "ohos.permission.APPROXIMATELY_LOCATION",
"ohos.permission.CAMERA"
]
}
;
Expand Down
43 changes: 20 additions & 23 deletions harmony/camera_kit/src/main/ets/service/CameraService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class CameraService {
public photoPreviewScale: number = 1; // 预览缩放倍数
private cameraDeviceIndex: number = 0;
private surfaceId: string = '';
private outPath: string = 'photo'
private outPathArray: string[] = ['photo', 'video'];
private basicPath: string = '';


// 推荐拍照分辨率之一
Expand Down Expand Up @@ -71,6 +72,7 @@ class CameraService {

constructor() {
this.phAccessHelper = photoAccessHelper.getPhotoAccessHelper(this.context);
this.basicPath = this.context.filesDir;
}

/**
Expand Down Expand Up @@ -233,7 +235,7 @@ class CameraService {
previewProfile.format === this.previewProfileObj.format;
});
if (index === -1) {
return undefined;
return previewProfiles[0];
}
return previewProfiles[index];
}
Expand All @@ -252,7 +254,7 @@ class CameraService {
photoProfile.format === this.photoProfileObj.format;
});
if (index === -1) {
return undefined;
return photoProfiles[0];
}
return photoProfiles[index];
}
Expand Down Expand Up @@ -362,17 +364,14 @@ class CameraService {
async takePicture(): Promise<CaptureData> {
Logger.info(TAG, 'takePicture start');
this.photoAsset.uri = '';
const location = await this.getLocation();
if (location) {
this.photoCaptureSetting.location = location;
}
this.photoAsset.path = '';
await this.photoOutput?.capture(this.photoCaptureSetting);
this.photoAsset.width = this.previewProfileObj.size.width;
this.photoAsset.height = this.previewProfileObj.size.height;
Logger.info(TAG, 'takePicture end');
return new Promise((resolve) => {
const timer = setInterval(() => {
if (this.photoAsset.uri) {
if (this.photoAsset.uri && this.photoAsset.path) {
clearInterval(timer);
resolve(this.photoAsset);
}
Expand Down Expand Up @@ -593,24 +592,26 @@ class CameraService {
* 保存照片
* */
async savePicture(photoAccess: photoAccessHelper.PhotoAsset): Promise<void> {
let photoFile = `${this.outPath}/${Date.now().toString()}.jpeg`;
photoFile = await this.getMediaLibraryUri(photoFile, `${Date.now()}`, 'jpeg', photoAccessHelper.PhotoType.IMAGE)
// 获取文件buffer
let file = fs.openSync(photoAccess.uri, fs.OpenMode.READ_ONLY)
let stat = fs.statSync(file.fd)
let photoFile = `${this.basicPath}/${this.outPathArray[0]}/${Date.now().toString()}.jpeg`;
// photoFile = await this.getMediaLibraryUri(photoFile, `${Date.now()}`, 'jpeg', photoAccessHelper.PhotoType.IMAGE)
// 根据相机拍照图片路径,获取文件buffer
let file = fs.openSync(photoAccess.uri, fs.OpenMode.READ_ONLY);
let stat = fs.statSync(file.fd);
let buffer = new ArrayBuffer(stat.size);
fs.readSync(file.fd, buffer)
fs.fsyncSync(file.fd)
fs.closeSync(file)
fs.readSync(file.fd, buffer);
fs.fsyncSync(file.fd);
fs.closeSync(file);
try {
file = fs.openSync(photoFile, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
await fs.write(file.fd, buffer);
} catch (error) {
Logger.error(TAG, `savePicture statSync failed,code:${error}.`);
}
fs.closeSync(file);
this.photoAsset.uri = photoFile;
this.photoAsset.name = photoAccess.get('display_name') as string;
Logger.info(TAG, `photoFile:${JSON.stringify(photoFile)}`);
this.photoAsset.path = photoFile;//沙箱路径
this.photoAsset.uri = photoAccess.uri;//媒体路径
this.photoAsset.name = photoAccess.displayName;
}

/**
Expand Down Expand Up @@ -642,11 +643,7 @@ class CameraService {
Logger.error(TAG, 'photoAsset is undefined');
return;
}
const uri = photoAsset.get('uri') as string;
const name = photoAsset.get('display_name') as string;
this.photoAsset.uri = uri;
this.photoAsset.name = name;
// this.savePicture(photoAsset);
this.savePicture(photoAsset);
});
} catch (err) {
Logger.error(TAG, 'photoOutputCallBack error');
Expand Down
16 changes: 1 addition & 15 deletions harmony/camera_kit/src/main/module.json5
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,7 @@
"usedScene": {
"when": "always"
}
},
{
"name": "ohos.permission.MICROPHONE",
"reason": "$string:microphone_reason",
"usedScene": {
"when": "always"
}
},
{
"name": "ohos.permission.APPROXIMATELY_LOCATION",
"reason": "$string:location_reason",
"usedScene": {
"when": "always"
}
},
}
]
}
}