获取运动轨迹路线,可以使用Map Kit或Canvas绘制轨迹路线后将图片保存,两种方案的实现参考如下。
// Trace是组件id标识,仅作参考,实际请以需要截图的组件为准
let pixelMap = this.uiContext.getComponentSnapshot().getSync('Trace', { scale: 1, waitUntilRenderFinished: true });
if (pixelMap !== undefined) {
this.savePixelToSandbox(pixelMap);
} async savePixelToSandbox(pixel: image.PixelMap | undefined) {
let imageFile: fs.File | null = null;
try {
let context = this.uiContext.getHostContext();
let cacheDir = context?.filesDir;
let filePath = `${cacheDir}` + '/test.png';
imageFile = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 绘制内容写进图片文件里。
await image.createImagePacker().packToFile(pixel, imageFile.fd, {
format: 'image/png',
quality: 80
}).then(() => {
console.info(`Success to save image`);
}).catch((error: BusinessError) => {
console.error(`Failed to pack the image to file.code ${error.code},message is ${error.message}`);
});
} catch (err) {
console.error(`Failed to save image: ${err.message}`);
} finally {
if (imageFile) {
fs.closeSync(imageFile);
}
}
} private onDraw = () => {
this.context.lineCap = this.lineCap;
if (this.linesStyle != undefined) {
// 设置轨迹图的颜色
this.context.strokeStyle = this.linesStyle;
this.context.lineWidth = this.axisWidth;
// 开始绘制,根据坐标自定义路径
this.context.beginPath();
this.context.moveTo(30, 30);
this.context.lineTo(60, 80);
this.context.lineTo(100, 50);
this.context.lineTo(200, 320);
this.context.lineTo(240, 360);
// 结束绘制轨迹图
this.context.stroke();
this.pixelmap = this.context.getPixelMap(0, 0, 400, 400); // 创建PixelMap对象
}
}; this.pixelmap = this.context.getPixelMap(0, 0, 400, 400); // 创建PixelMap对象
完整示例参考如下:
import image from '@ohos.multimedia.image';
import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct GetTracePixel {
@State pixelmap: image.PixelMap | undefined = undefined;
private settings: RenderingContextSettings = new RenderingContextSettings(true);
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings);
public lineCap: CanvasLineCap = 'round';
// 绘制轨迹的线条宽度
public axisWidth: number = 6;
// 轨迹图样式
public linesStyle?: string | CanvasGradient | CanvasPattern = '#ff0099ff';
private uiContext = this.getUIContext();
async savePixelToSandbox(pixel: image.PixelMap | undefined) {
let imageFile: fs.File | null = null;
try {
let context = this.uiContext.getHostContext();
let cacheDir = context?.filesDir;
let filePath = `${cacheDir}` + '/test.png';
imageFile = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 绘制内容写进图片文件里。
await image.createImagePacker().packToFile(pixel, imageFile.fd, {
format: 'image/png',
quality: 80
}).then(() => {
console.info(`Success to save image`);
}).catch((error: BusinessError) => {
console.error(`Failed to pack the image to file.code ${error.code},message is ${error.message}`);
});
} catch (err) {
console.error(`Failed to save image: ${err.message}`);
} finally {
if (imageFile) {
fs.closeSync(imageFile);
}
}
}
build() {
Column({space:20}) {
Canvas(this.context)
.width('70%')
.height('70%')
.onReady(this.onDraw)
.id('Trace');
Button('保存PixelMap至应用沙箱')
.onClick(() => {
if (this.pixelmap !== undefined) {
this.savePixelToSandbox(this.pixelmap);
}
});
Button('组件截图保存')
.onClick(() => {
// Trace是组件id标识,仅作参考,实际请以需要截图的组件为准
let pixelMap = this.uiContext.getComponentSnapshot().getSync('Trace', { scale: 1, waitUntilRenderFinished: true });
if (pixelMap !== undefined) {
this.savePixelToSandbox(pixelMap);
}
});
}
.width('100%')
.height('100%');
}
private onDraw = () => {
this.context.lineCap = this.lineCap;
if (this.linesStyle != undefined) {
// 设置轨迹图的颜色
this.context.strokeStyle = this.linesStyle;
this.context.lineWidth = this.axisWidth;
// 开始绘制,根据坐标自定义路径
this.context.beginPath();
this.context.moveTo(30, 30);
this.context.lineTo(60, 80);
this.context.lineTo(100, 50);
this.context.lineTo(200, 320);
this.context.lineTo(240, 360);
// 结束绘制轨迹图
this.context.stroke();
this.pixelmap = this.context.getPixelMap(0, 0, 400, 400); // 创建PixelMap对象
}
};
}