这是一个基于AI基础视觉服务实现的背景替换案例,通过调用设备相册选择图片后对主体进行智能分割,并支持动态更换背景颜色。
import { photoAccessHelper } from '@kit.MediaLibraryKit'
import { fileIo } from '@kit.CoreFileKit'
import image from '@ohos.multimedia.image'
import { subjectSegmentation } from '@kit.CoreVisionKit'
import { promptAction } from '@kit.ArkUI'
@Entry
@ComponentV2
struct SubjectSegmentation {
@Local chooseImage?: PixelMap // 原始图片
@Local segmentedImage?: PixelMap // 分割后图片
@Local bgColor: ResourceColor = Color.White // 背景色状态
async segmentImage() {
if (canIUse('SystemCapability.AI.Vision.SubjectSegmentation')) {
// 创建图片选择器
const photoPicker: photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker();
// 选择单张图片
const photoResult = await photoPicker.select({
MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE,
maxSelectNumber: 1
})
// 获取图片URI并转换为PixelMap格式
const photoUri = photoResult.photoUris[0]
const fileSource = await fileIo.open(photoUri, fileIo.OpenMode.READ_ONLY);
const imageSource = image.createImageSource(fileSource.fd);
this.chooseImage = await imageSource.createPixelMap();
// 配置视觉识别参数
const visionInfo: subjectSegmentation.VisionInfo = {
pixelMap: this.chooseImage,
};
try {
// 执行主体分割
const result = await subjectSegmentation.doSegmentation(visionInfo, {
enableSubjectForegroundImage: true
})
this.segmentedImage = result.fullSubject.foregroundImage
} catch (e) {
promptAction.showToast({ message: e.message })
}
}
}
build() {
Column({ space: 20 }) {
// 原始图片展示区域
Text('原图:')
Image(this.chooseImage)
.objectFit(ImageFit.Fill)
.height('30%')
// 分割后图片展示区域
Text('扣除背景:')
Image(this.segmentedImage)
.objectFit(ImageFit.Fill)
.height('30%')
.backgroundColor(this.bgColor)
// 功能操作按钮
Button('选择图片').onClick(() => this.segmentImage())
Button('更换背景').onClick(() => {
// 生成随机RGB背景色
const a = Math.round(Math.random() * 255)
const b = Math.round(Math.random() * 255)
const c = Math.round(Math.random() * 255)
this.bgColor = `rgb(${a},${b},${c})`
})
}
.padding(15)
.height('100%')
.width('100%')
}
}
// 此处完整保留用户提供的原始代码
import { photoAccessHelper } from '@kit.MediaLibraryKit'
import { fileIo } from '@kit.CoreFileKit'
import image from '@ohos.multimedia.image'
import { subjectSegmentation } from '@kit.CoreVisionKit'
import { promptAction } from '@kit.ArkUI'
@Entry
@ComponentV2
struct SubjectSegmentation {
@Local chooseImage?: PixelMap
@Local segmentedImage?: PixelMap
@Local bgColor: ResourceColor = Color.White
async segmentImage() {
if (canIUse('SystemCapability.AI.Vision.SubjectSegmentation')) {
const photoPicker: photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker();
const photoResult = await photoPicker.select({
MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE,
maxSelectNumber: 1
})
const photoUri = photoResult.photoUris[0]
const fileSource = await fileIo.open(photoUri, fileIo.OpenMode.READ_ONLY);
const imageSource = image.createImageSource(fileSource.fd);
this.chooseImage = await imageSource.createPixelMap();
const visionInfo: subjectSegmentation.VisionInfo = {
pixelMap: this.chooseImage,
};
try {
const result = await subjectSegmentation.doSegmentation(visionInfo, {
enableSubjectForegroundImage: true
})
this.segmentedImage = result.fullSubject.foregroundImage
} catch (e) {
promptAction.showToast({ message: e.message })
}
}
}
build() {
Column({ space: 20 }) {
Text('原图:')
Image(this.chooseImage)
.objectFit(ImageFit.Fill)
.height('30%')
Text('扣除背景:')
Image(this.segmentedImage)
.objectFit(ImageFit.Fill)
.height('30%')
.backgroundColor(this.bgColor)
Button('选择图片')
.onClick(() => this.segmentImage())
Button('更换背景')
.onClick(() => {
const a = Math.round(Math.random() * 255)
const b = Math.round(Math.random() * 255)
const c = Math.round(Math.random() * 255)
this.bgColor = `rgb(${a},${b},${c})`
})
}
.padding(15)
.height('100%')
.width('100%')
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有