HarmonyOS 鸿蒙Next 相册/拍照 压缩 上传 能提供一下官方demo吗
HarmonyOS 鸿蒙Next 相册/拍照 压缩 上传 能提供一下官方demo吗
拍照上传头像demo:
import { camera, cameraPicker } from '@kit.CameraKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { picker } from '@kit.CoreFileKit';
@Entry
@Component
struct Index {
@State message: string = 'Hello World';
@State uri: string = '';
private cameraPosition: Array<camera.CameraPosition> = [
camera.CameraPosition.CAMERA_POSITION_UNSPECIFIED, camera.CameraPosition.CAMERA_POSITION_BACK,
camera.CameraPosition.CAMERA_POSITION_FRONT, camera.CameraPosition.CAMERA_POSITION_FOLD_INNER
];
private mediaType: Array<cameraPicker.PickerMediaType> = [
cameraPicker.PickerMediaType.PHOTO, cameraPicker.PickerMediaType.VIDEO
];
async open() {
try {
// Configure to launch the rear camera
let pickerProfile: cameraPicker.PickerProfile = { cameraPosition: this.cameraPosition[1] };
// Configure to photo mode
let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(getContext(this),
[this.mediaType[0]], pickerProfile);
// Get video URI
this.uri = pickerResult.resultUri;
hilog.info(0x0000, ' ', "the pick pickerResult is:" + JSON.stringify(pickerResult));
} catch (error) {
let err = error as BusinessError;
hilog.error(0x0000, '', `the pick call failed. error code: ${err.code}`);
}
}
async choose() {
try {
// 设置图片选择器选项
const photoSelectOptions = new picker.PhotoSelectOptions();
// 限制只能选择一张图片
photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
photoSelectOptions.maxSelectNumber = 10;
// 创建并实例化图片选择器
const photoViewPicker = new picker.PhotoViewPicker();
// 选择图片并获取图片URI
let uris: picker.PhotoSelectResult = await photoViewPicker.select(photoSelectOptions);
if (!uris || uris.photoUris.length === 0) {
return;
}
console.info('123123uris', JSON.stringify(uris))
// 获取选中图片的第一张URI
this.uri = uris.photoUris[0];
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
}
}
build() {
Column() {
Image(this.uri)
.width(200)
.height(200)
.borderRadius(100)
.backgroundColor(Color.Gray)
Button('打开相机').onClick(() => {
this.open()
})
Button('选择图片').onClick(() => {
this.choose()
})
}
.justifyContent(FlexAlign.Start)
.alignItems(HorizontalAlign.Center)
.height('100%')
.width('100%')
}
}
关于图片压缩可以使用ImagePacker
参考官方文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-image-V5#imagepacker
选择相册图片将文件保存到应用沙箱目录。
参考以下demo:
import picker from '@ohos.multimedia.cameraPicker'
import camera from '@ohos.multimedia.camera';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import fileuri from '@ohos.file.fileuri';
import fs from '@ohos.file.fs';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
let context = getContext(this) as common.Context;
class CameraPosition {
cameraPosition: camera.CameraPosition
saveUri: string
constructor(cameraPosition: camera.CameraPosition, saveUri: string) {
this.cameraPosition = cameraPosition
this.saveUri = saveUri
}
}
let pathDir = getContext().filesDir;
console.log('保存路径为' + pathDir)
let filePath = pathDir + '/picture.jpg'
fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE);
let uri = fileuri.getUriFromPath(filePath);
async function photo() {
try {
let pickerProfile = new CameraPosition(camera.CameraPosition.CAMERA_POSITION_BACK, uri)
let pickerResult: picker.PickerResult = await picker.pick(context,
[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.code}`);
}
}
async function picture() {
let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 1;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
let photouri: Array<string> = PhotoSelectResult.photoUris
let file = fs.openSync(photouri[0], fs.OpenMode.READ_ONLY)
let file2 = fs.openSync(pathDir + '/picture2.jpg', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
fs.copyFileSync(file.fd, file2.fd)
fs.closeSync(file);
fs.closeSync(file2);
})
}
@Entry
@Component
export struct Index {
build() {
Column() {
Button('选择并保存').onClick(() => {
picture()
})
Button('拍照并保存').onClick(() => {
photo()
})
}
}
}
Image组件不能直接传入应用沙箱路径,需要传入应用沙箱uri。
https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkui-154-V5
将PixelMap压缩到指定大小以下
https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-image-16-V5
更多关于HarmonyOS 鸿蒙Next 相册/拍照 压缩 上传 能提供一下官方demo吗的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
HarmonyOS 鸿蒙Next平台针对相册/拍照、压缩及上传功能,官方并未直接提供一个统一的demo来涵盖所有步骤,因为这类功能通常涉及多个模块和API的综合运用。不过,可以通过以下途径获取相关示例代码或文档:
-
HarmonyOS开发者官网:访问HarmonyOS开发者官网,在“开发文档”或“示例代码”区域,可以找到针对相册访问、拍照功能、文件压缩以及网络上传等功能的独立模块示例。你可以根据这些示例代码,结合实际需求进行组合和修改。
-
API参考:在HarmonyOS的API参考文档中,详细列出了各个功能的接口说明和使用方法。通过查阅这些文档,可以了解如何实现相册访问、拍照、图片压缩及上传等功能的具体步骤。
-
Sample Code仓库:HarmonyOS官方可能在一些开源平台上提供了更丰富的示例代码,可以搜索并关注这些仓库,以获取更多实用的代码片段和解决方案。
请注意,由于HarmonyOS平台不断更新迭代,建议定期访问开发者官网以获取最新的开发文档和示例代码。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html