硬件限制因平台而异,可以通过如下命令进行查看:
hdc shell hidumper -s 10 -a 'vktextureLimit'
常见值为“width: 8192 height: 8192”,表示最大绘制纹理尺寸的长宽都需要在8192像素以内。比较待截图组件的尺寸,以便确认截图失败是否为该原因导致,如果是,请调整所截图组件的大小,或实现为滚动截图后自行拼接。实现请参考截取长内容(滚动截图)和长截图。
如需实现离屏组件的长截图,可参考以下实现:
// src/main/ets/utils/Utils.ets
import { image } from '@kit.ImageKit';
import { BusinessError } from '@kit.BasicServicesKit';
export class Utils {
static sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Calculate the valid screenshot area
static async getSnapshotArea(context: UIContext, pixelMap: PixelMap, scrollYOffsets: number[], listWidth: number,
listHeight: number): Promise<image.PositionArea> {
let stride = pixelMap.getBytesNumberPerRow();
let bytesNumber = pixelMap.getPixelBytesNumber();
let buffer: ArrayBuffer = new ArrayBuffer(bytesNumber);
let len = scrollYOffsets.length;
if (scrollYOffsets.length >= 2) {
let realScrollHeight = scrollYOffsets[len-1] - scrollYOffsets[len-2];
if (listHeight - realScrollHeight > 0) {
let cropRegion: image.Region = {
x: 0,
y: context.vp2px(listHeight - realScrollHeight) || 0,
size: {
height: context.vp2px(realScrollHeight) || 0,
width: context.vp2px(listWidth) || 0
}
};
await pixelMap.crop(cropRegion);
}
}
let area: image.PositionArea = {
pixels: buffer,
offset: 0,
stride: stride,
region: {
size: {
width: 0,
height: 0
},
x: 0,
y: 0
}
}
try {
let imgInfo = pixelMap.getImageInfoSync();
area.region.size.width = imgInfo.size.width;
area.region.size.height = imgInfo.size.height;
pixelMap.readPixelsSync(area);
} catch (err) {
let error = err as BusinessError;
console.error(`getSnapshotArea err, code:${error.code}, message: ${error.message}`);
}
return area;
}
// Graphic splicing
static async mergeImage(context: UIContext, areaArray: image.PositionArea[], lastOffsetY: number, listWidth: number,
listHeight: number): Promise<PixelMap> {
let opts: image.InitializationOptions = {
editable: true,
pixelFormat: 4,
size: {
width: context.vp2px(listWidth) || 0,
height: context.vp2px(lastOffsetY + listHeight) || 0
}
};
let longPixelMap = image.createPixelMapSync(opts);
let imgPosition: number = 0;
for (let i = 0; i < areaArray.length; i++) {
let readArea = areaArray[i];
let area: image.PositionArea = {
pixels: readArea.pixels,
offset: 0,
stride: readArea.stride,
region: {
size: {
width: readArea.region.size.width,
height: readArea.region.size.height
},
x: 0,
y: imgPosition
}
}
imgPosition += readArea.region.size.height;
try {
longPixelMap.writePixelsSync(area);
} catch (err) {
let error = err as BusinessError;
console.error(`writePixelsSync err, code:${error.code}, message: ${error.message}`);
}
}
return longPixelMap;
}
}
// src/main/ets/pages/Index.ets
import { image } from '@kit.ImageKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { Utils } from '../utils/Utils';
export class MyDataSource {
private _data: string[] = [];
private _listeners: DataChangeListener[] = [];
pushData(data: string): void {
this._data.push(data);
this._listeners.forEach(listener => {
listener.onDataAdd(this._data.length - 1);
})
}
getAllData(): string[] {
return this._data;
}
totalCount(): number {
return this._data.length;
}
getData(index: number): string {
return this._data[index];
}
registerDataChangeListener(listener: DataChangeListener): void {
this._listeners.push(listener);
}
unregisterDataChangeListener(listener: DataChangeListener): void {
const index = this._listeners.indexOf(listener);
if (index != -1) {
this._listeners.splice(index, 1);
}
}
}
@Entry
@Component
struct SnapshotExample {
private scroller: Scroller = new Scroller();
private listComponentWidth: number = 0;
private listComponentHeight: number = 0;
@State mergedImage: PixelMap | undefined = undefined;
private areaArray: image.PositionArea[] = [];
private scrollYOffsets: number[] = [];
private data: MyDataSource = new MyDataSource();
private listId: string = 'LIST_ID';
aboutToAppear(): void {
for (let i = 0; i < 50; i++) {
this.data.pushData(Hello ${i});
}
}
async onceSnapshot() {
await this.beforeSnapshot();
await this.snapAndMerge();
this.afterGeneratorImage();
}
async snapAndMerge() {
try {
// Record the current scrolling position
this.scrollYOffsets.push(this.scroller.currentOffset().yOffset);
// Take a screenshot of the current display part of the component
const pixelMap = await this.getUIContext().getComponentSnapshot().get(this.listId);
// Calculate the valid screenshot area
let area: image.PositionArea =
await Utils.getSnapshotArea(this.getUIContext(), pixelMap, this.scrollYOffsets, this.listComponentWidth,
this.listComponentHeight);
this.areaArray.push(area);
// Determine whether to scroll to the bottom
if (!this.scroller.isAtEnd()) {
// Not to the bottom: Scroll down by one screen height
this.scroller.scrollTo({
xOffset: 0,
yOffset: (this.scroller.currentOffset().yOffset + this.listComponentHeight),
animation: {
duration: 200
}
});
await Utils.sleep(200);
await this.snapAndMerge();
} else {
this.mergedImage =
await Utils.mergeImage(this.getUIContext(), this.areaArray, this.scrollYOffsets[this.scrollYOffsets.length-1],
this.listComponentWidth, this.listComponentHeight);
}
} catch (err) {
let error = err as BusinessError;
console.error(snapAndMerge err, code:${error.code}, message: ${error.message});
}
}
async beforeSnapshot() {
try {
this.scroller.scrollTo({
xOffset: 0,
yOffset: 0,
animation: {
duration: 200
}
});
await Utils.sleep(200);
} catch (err) {
let error = err as BusinessError;
console.error(beforeSnapshot err, code:${error.code}, message: ${error.message});
}
}
afterGeneratorImage() {
this.scrollYOffsets.length = 0;
this.areaArray.length = 0;
}
build() {
Column({ space: 12 }) {
Button('Click to get the snapshot')
.onClick(() => {
this.onceSnapshot();
})
Stack() {
// Screenshot component
List({ space: 12, scroller: this.scroller }) {
LazyForEach(this.data, (item: string) => {
ListItem() {
Row() {
Text(item)
.fontSize(50)
.height(50)
}
}
}, (item: number) => item.toString())
}
.scrollBar(BarState.Off)
.cachedCount(3)
.width('100%')
.height('100%')
.backgroundColor($r('sys.color.background_secondary'))
.id(this.listId)
.onAreaChange((oldValue, newValue) => {
this.listComponentWidth = newValue.width as number;
this.listComponentHeight = newValue.height as number;
})
// Set the Z-sequence to -1 to ensure that this component is invisible
.zIndex(-1)
// Use a mask to cover the screenshot area
Column()
.width('100%').height('100%').backgroundColor(Color.White)
// Long screenshot
Scroll() {
Image(this.mergedImage)
}
}
.width('100%')
.layoutWeight(1)
}
}
}
https://sites.google.com/view/4qpzqf30/home
https://sites.google.com/view/1ggrec61/home
https://sites.google.com/view/7dqpdy74/home
https://sites.google.com/view/5bcwfb15/home
https://sites.google.com/view/3ccqme12/home
https://sites.google.com/view/9fauxq30/home
https://sites.google.com/view/0cskdu29/home
https://sites.google.com/view/7ndhuv19/home
https://sites.google.com/view/2qfdbs75/home
https://sites.google.com/view/7tbfwn54/home
https://sites.google.com/view/8utwqh99/home
https://sites.google.com/view/8tmfsn03/home
https://sites.google.com/view/7hyniw30/home
https://sites.google.com/view/5vcrza32/home
https://sites.google.com/view/9bicoh33/home
https://sites.google.com/view/8mykyr89/home
https://sites.google.com/view/9dkwex15/home
https://sites.google.com/view/3jdidd23/home
https://sites.google.com/view/4jnbko13/home
https://sites.google.com/view/3hiqwy37/home
https://sites.google.com/view/9sdhji16/home
https://sites.google.com/view/9vjvuk08/home
https://sites.google.com/view/4vtgzg10/home
https://sites.google.com/view/9simwj81/home
https://sites.google.com/view/2fhkhe54/home
https://sites.google.com/view/3ifqol91/home
https://sites.google.com/view/5oaygy48/home
https://sites.google.com/view/9uzuby80/home
https://sites.google.com/view/4mfobg52/home
https://sites.google.com/view/7lmnbx80/home
https://sites.google.com/view/7zieer80/home
https://sites.google.com/view/2wsnxl35/home
https://sites.google.com/view/9upiju57/home
https://sites.google.com/view/0cjgci51/home
https://sites.google.com/view/6bbfhi31/home
https://sites.google.com/view/5kfsgr23/home
https://sites.google.com/view/2yahzy66/home
https://sites.google.com/view/3bjdfe66/home
https://sites.google.com/view/8zysdc97/home
https://sites.google.com/view/4whfwi14/home
https://sites.google.com/view/4oxzdw14/home
https://sites.google.com/view/3xtxxz40/home
https://sites.google.com/view/3zfzrk48/home
https://sites.google.com/view/1vuurx26/home
https://sites.google.com/view/5fqasw51/home
https://sites.google.com/view/4auyde58/home
https://sites.google.com/view/3gztws07/home
https://sites.google.com/view/1unpun49/home
https://sites.google.com/view/3jclpx68/home
https://sites.google.com/view/8vfpdb61/home
https://sites.google.com/view/1jskfv65/home
https://sites.google.com/view/8khlye43/home
https://sites.google.com/view/0jlpsf46/home
https://sites.google.com/view/4rrmuk82/home
https://sites.google.com/view/8pyjpk92/home
https://sites.google.com/view/6skokh74/home
https://sites.google.com/view/6xhhhj64/home
https://sites.google.com/view/3zdgnv39/home
https://sites.google.com/view/3krsoe29/home
https://sites.google.com/view/4qccqa54/home
https://sites.google.com/view/6bmcza90/home
https://sites.google.com/view/9myclp29/home
https://sites.google.com/view/8chakc99/home
https://sites.google.com/view/7naelf98/home
https://sites.google.com/view/9ovzhg81/home
https://sites.google.com/view/3yvfkx78/home
https://sites.google.com/view/5cdvsn19/home
https://sites.google.com/view/1huyew52/home
https://sites.google.com/view/8lgdoh08/home
https://sites.google.com/view/2nqdur07/home
https://sites.google.com/view/5scjnw53/home
https://sites.google.com/view/9xvbba99/home
https://sites.google.com/view/3nqkgc40/home
https://sites.google.com/view/8gvcqc86/home
https://sites.google.com/view/9qpetf93/home
https://sites.google.com/view/7gkigb30/home
https://sites.google.com/view/9ghmex19/home
https://sites.google.com/view/1wcrpp04/home
https://sites.google.com/view/8udpqi35/home
https://sites.google.com/view/3canhn37/home
https://sites.google.com/view/7emxvq67/home
https://sites.google.com/view/1glhsm08/home
https://sites.google.com/view/1rbpie37/home
https://sites.google.com/view/6lknzd58/home
https://sites.google.com/view/0wjrrz98/home
https://sites.google.com/view/1kozsf11/home
https://sites.google.com/view/6hjwzy13/home
https://sites.google.com/view/9hhvpx75/home
https://sites.google.com/view/2wnusu60/home
https://sites.google.com/view/5dwcsn95/home
https://sites.google.com/view/8nwpaq06/home
0 个评论
要回复文章请先登录或注册