
鸿蒙自动打码截图工具开发指南 原创
鸿蒙自动打码截图工具开发指南(安全版)
一、系统架构设计
基于HarmonyOS的自动打码截图工具,利用AI能力识别特定区域并自动处理,支持多设备协同工作:
智能识别:通过@ohos.ai模块识别特定视觉元素
自动处理:对识别到的区域进行模糊/马赛克处理
跨设备同步:通过分布式能力实现多设备协同
数据安全:所有处理在设备端完成
!https://example.com/harmony-safe-processing-arch.png
二、核心代码实现
区域识别服务
// RegionDetectionService.ets
import visual from ‘@ohos.ai.visual’;
import image from ‘@ohos.multimedia.image’;
class RegionDetectionService {
private static instance: RegionDetectionService = null;
private detector: visual.VisualDetector;
private constructor() {
this.initDetector();
public static getInstance(): RegionDetectionService {
if (!RegionDetectionService.instance) {
RegionDetectionService.instance = new RegionDetectionService();
return RegionDetectionService.instance;
private async initDetector(): Promise<void> {
try {
const context = getContext() as common.Context;
this.detector = await visual.createVisualDetector(context);
await this.detector.setConfig({
detectMode: visual.DetectMode.DETECT_MODE_GENERAL,
performanceMode: visual.PerformanceMode.PERFORMANCE_MODE_BALANCED
});
catch (err) {
console.error('初始化检测器失败:', JSON.stringify(err));
}
public async detectRegions(pixelMap: image.PixelMap): Promise<RegionResult> {
const result: RegionResult = {
regions: []
};
try {
const detectResults = await this.detector.detect(pixelMap);
result.regions = detectResults.regionList.map(region => ({
x: region.rect.left,
y: region.rect.top,
width: region.rect.right - region.rect.left,
height: region.rect.bottom - region.rect.top,
confidence: region.confidence
}));
catch (err) {
console.error('检测区域失败:', JSON.stringify(err));
return result;
public async release(): Promise<void> {
if (this.detector) {
await this.detector.release();
this.detector = null;
}
interface RegionResult {
regions: Region[];
interface Region {
x: number;
y: number;
width: number;
height: number;
confidence: number;
export const regionService = RegionDetectionService.getInstance();
图像处理服务
// ImageProcessingService.ets
import image from ‘@ohos.multimedia.image’;
import effect from ‘@ohos.effectKit’;
class ImageProcessingService {
private static instance: ImageProcessingService = null;
private constructor() {}
public static getInstance(): ImageProcessingService {
if (!ImageProcessingService.instance) {
ImageProcessingService.instance = new ImageProcessingService();
return ImageProcessingService.instance;
public async applyEffect(
pixelMap: image.PixelMap,
areas: Region[],
effectType: 'mosaic' | 'blur',
effectSize: number = 15
): Promise<image.PixelMap> {
if (areas.length === 0) return pixelMap;
try {
const processor = new effect.EffectProcessor();
let processedPixelMap = pixelMap;
for (const area of areas) {
if (area.width <= 0 || area.height <= 0) continue;
let effectConfig;
if (effectType === 'mosaic') {
effectConfig = new effect.MosaicEffect({
x: area.x,
y: area.y,
width: area.width,
height: area.height,
mosaicSize: effectSize
});
else {
effectConfig = new effect.BlurEffect({
x: area.x,
y: area.y,
width: area.width,
height: area.height,
radius: effectSize
});
processedPixelMap = await processor.applyEffect(
processedPixelMap,
[effectConfig]
);
return processedPixelMap;
catch (err) {
console.error('应用效果失败:', JSON.stringify(err));
return pixelMap;
}
public async captureScreen(): Promise<image.PixelMap | null> {
try {
const window = window.getTopWindow(getContext(this));
return await window.screenshot();
catch (err) {
console.error('截屏失败:', JSON.stringify(err));
return null;
}
public async saveImage(pixelMap: image.PixelMap): Promise<string | null> {
try {
const mediaLib = await mediaLibrary.getMediaLibrary(getContext(this));
const file = await mediaLib.createAsset(
mediaLibrary.MediaType.IMAGE,
‘processed_’ + Date.now() + ‘.jpg’,
mediaLibrary.StorageFlag.STORAGE_FLAG_DEFAULT
);
const fd = await file.open('w');
const arrayBuffer = await image.createImagePacker().packing(pixelMap, {
format: 'image/jpeg',
quality: 90
});
fs.writeSync(fd, arrayBuffer);
await file.close(fd);
return file.uri;
catch (err) {
console.error('保存图像失败:', JSON.stringify(err));
return null;
}
export const imageService = ImageProcessingService.getInstance();
主界面实现
// MainScreen.ets
import { regionService } from ‘./RegionDetectionService’;
import { imageService } from ‘./ImageProcessingService’;
@Component
export struct MainScreen {
@State originalImage: image.PixelMap | null = null;
@State processedImage: image.PixelMap | null = null;
@State detectedRegions: Region[] = [];
@State isProcessing: boolean = false;
@State effectSize: number = 15;
@State effectType: ‘mosaic’ | ‘blur’ = ‘mosaic’;
build() {
Column() {
// 标题
Text(‘图像处理工具’)
.fontSize(24)
.fontWeight(FontWeight.Bold)
.margin({ bottom: 20 })
// 图像显示区域
Row() {
if (this.originalImage) {
Column() {
Text('原始图像')
.fontSize(16)
.margin({ bottom: 8 })
Image(this.originalImage)
.width('90%')
.height(200)
.objectFit(ImageFit.Contain)
.width(‘50%’)
if (this.processedImage) {
Column() {
Text('处理后')
.fontSize(16)
.margin({ bottom: 8 })
Image(this.processedImage)
.width('90%')
.height(200)
.objectFit(ImageFit.Contain)
.width(‘50%’)
}
.height(250)
.margin({ bottom: 20 })
// 处理选项
Column() {
Row() {
Text('处理方式:')
.fontSize(16)
.margin({ right: 10 })
Radio({ value: 'mosaic', group: 'effectType' })
.checked(this.effectType === 'mosaic')
.onChange((checked) => {
if (checked) this.effectType = 'mosaic';
})
Text('马赛克')
.fontSize(14)
.margin({ right: 15 })
Radio({ value: 'blur', group: 'effectType' })
.checked(this.effectType === 'blur')
.onChange((checked) => {
if (checked) this.effectType = 'blur';
})
Text('模糊')
.fontSize(14)
.margin({ bottom: 10 })
Row() {
Text(${this.effectType === 'mosaic' ? '马赛克' : '模糊'}强度:)
.fontSize(16)
.margin({ right: 10 })
Slider({
value: this.effectSize,
min: this.effectType === 'mosaic' ? 5 : 10,
max: this.effectType === 'mosaic' ? 30 : 50,
step: 1,
style: SliderStyle.OutSet
})
.onChange((value) => {
this.effectSize = value;
})
Text(this.effectSize.toString())
.fontSize(14)
.width(30)
}
.width('90%')
.padding(10)
.backgroundColor(Color.White)
.borderRadius(8)
.margin({ bottom: 20 })
// 控制按钮
Column() {
Button('截屏并处理')
.width('80%')
.height(50)
.margin({ bottom: 10 })
.onClick(() => {
this.captureAndProcess();
})
Button('保存图像')
.width('80%')
.height(50)
.enabled(!!this.processedImage)
.margin({ bottom: 10 })
.onClick(() => {
this.saveImage();
})
.margin({ bottom: 20 })
// 处理状态
if (this.isProcessing) {
Progress()
.width('80%')
.color(Color.Blue)
}
.width('100%')
.height('100%')
.padding(20)
private async captureAndProcess(): Promise<void> {
this.isProcessing = true;
try {
// 1. 截屏
this.originalImage = await imageService.captureScreen();
if (!this.originalImage) return;
// 2. 检测区域
const result = await regionService.detectRegions(this.originalImage);
this.detectedRegions = result.regions;
// 3. 应用处理
this.processedImage = await imageService.applyEffect(
this.originalImage,
this.detectedRegions,
this.effectType,
this.effectSize
);
catch (err) {
console.error('处理失败:', JSON.stringify(err));
prompt.showToast({ message: '处理失败' });
finally {
this.isProcessing = false;
}
private async saveImage(): Promise<void> {
if (!this.processedImage) return;
this.isProcessing = true;
try {
const uri = await imageService.saveImage(this.processedImage);
if (uri) {
prompt.showToast({ message: '图像已保存' });
} catch (err) {
console.error('保存失败:', JSON.stringify(err));
prompt.showToast({ message: '保存失败' });
finally {
this.isProcessing = false;
}
三、项目配置
权限配置
// module.json5
“module”: {
"requestPermissions": [
“name”: “ohos.permission.CAPTURE_SCREEN”,
"reason": "截取屏幕内容"
},
“name”: “ohos.permission.READ_MEDIA”,
"reason": "读取媒体文件"
},
“name”: “ohos.permission.WRITE_MEDIA”,
"reason": "保存处理后的图像"
},
“name”: “ohos.permission.INTERNET”,
"reason": "加载AI模型"
],
"abilities": [
“name”: “MainAbility”,
"type": "page",
"visible": true
]
}
四、总结与扩展
本图像处理工具实现了以下核心功能:
区域识别:检测图像中的特定区域
效果处理:提供马赛克和模糊两种处理方式
本地处理:所有操作在设备端完成
扩展方向:
多效果支持:添加更多处理效果
性能优化:提升处理速度和效率
历史记录:保存处理记录
分享功能:分享处理后的图像
通过HarmonyOS的能力,我们构建了一个安全、高效的图像处理工具。
