HarmonyOS鸿蒙Next中拍照时如何添加水印信息

HarmonyOS鸿蒙Next中拍照时如何添加水印信息 拍照时如何添加水印信息,麻烦提供示例代码

3 回复

拍照添加水印:

方案:使用cameraPicker拉起系统相机拍照,拿到返回的picker.PickerResult.resultUri后使用fs.openSync读出图片文件转化为pixelMap,使用image组件渲染在页面上,在保存图片的时候通过drawing.TextBlob.makeFromString方法绘制水印后保存

第一步:使用cameraPicker拉起系统相机

import picker from '@ohos.multimedia.cameraPicker';
import camera from '@ohos.multimedia.camera';

async openCamera() {
try {
let pickerProfile: picker.PickerProfile = {
cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK
};
let pickerResult: picker.PickerResult = await picker.pick(getContext(this),
[picker.PickerMediaType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile);
console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));
// ...
} catch (error) {
let err = error as BusinessError;
console.error(`the pick call failed. error code: ${err.message}`);
}
}

第二步:将相机拍摄的照片渲染在页面中,使用fs.readSync读出文件字节流,再使用image.createImageSource创建图片资源管理器获取到图片的pixelMap

@State pixelMap: image.PixelMap | null = null
async openCamera() {
try {
// ...
if (pickerResult.resultUri) {
this.showImage = true
}
let file = fs.openSync(pickerResult.resultUri)
let buffer = new ArrayBuffer(fs.statSync(file.fd).size)
fs.readSync(file.fd, buffer)
let imageSource: image.ImageSource = image.createImageSource(buffer)
imageSource.getImageInfo((err, value) => {
if (err) {
return;
}
let opts: image.DecodingOptions = {
editable: true,
desiredSize: {
height: value.size.height,
width: value.size.width
}
};
imageSource.createPixelMap(opts, async (err, pixelMap) => {
this.pixelMap = pixelMap

})
})
} catch (error) {
let err = error as BusinessError;
console.error(`the pick call failed. error code: ${err.message}`);
}
}

build() {
// ...
if(this.showImage) {
Image(this.pixelMap).width("100%")
// ...
}
}

第三步:使用drawing.TextBlob.makeFromString绘制水印后 使用SaveButton保存在系统相册中

SaveButton()
.onClick(async (event: ClickEvent, result: SaveButtonOnClickResult) => {
if (result == SaveButtonOnClickResult.SUCCESS) {
try {
let context = getContext(this);
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
let uri = await phAccessHelper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'png'); // 创建媒体文件
saveToFile(this.addWaterMask(this.pixelMap!), uri);
} catch (err) {
console.error('createAsset failed, message = ', err);
}
} else {
console.error('SaveButtonOnClickResult createAsset failed');
}
})

更多关于HarmonyOS鸿蒙Next中拍照时如何添加水印信息的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,拍照时添加水印信息可以通过调用系统提供的相机API实现。首先,使用CameraKitCameraAbility初始化相机。然后,通过CaptureSession配置相机的参数,包括分辨率、对焦模式等。在拍照前,使用ImageReceiverSurface获取预览帧,并通过CanvasImage对象在预览帧上绘制水印信息。水印可以是文字、图片或时间戳等内容。绘制完成后,调用CaptureSession.capture()方法拍照,系统会将水印信息与拍摄的照片合成。最后,保存或处理带有水印的照片。具体实现代码可以参考鸿蒙开发者文档中的相机模块示例。

在HarmonyOS鸿蒙Next中,拍照时添加水印信息的步骤如下:

  1. 打开相机应用。
  2. 进入相机设置(通常通过右上角的齿轮图标)。
  3. 在设置中找到“水印”选项并启用。
  4. 选择或自定义水印内容,如时间、地点、设备型号等。
  5. 返回拍照界面,水印将自动添加到拍摄的照片中。

确保相机应用和系统版本支持水印功能,部分设备可能需要更新至最新版本。

回到顶部