HarmonyOS 鸿蒙Next image.PixelMap裁剪始终裁剪不到框选的位置
HarmonyOS 鸿蒙Next image.PixelMap裁剪始终裁剪不到框选的位置 需求场景: 在页面中有一块区域
this.displayPictureWidth = px2vp(displayClass.width * 0.95)
this.displayPictureHeight = px2vp(displayClass.height) - 280 - CommonConstant.bottomRectHeight - CommonConstant.StatusBarHeight
在上面这块区域中我使用
Canvas(this.canvasContext)
.width(this.displayPictureWidth)
.height(this.displayPictureHeight)
.onReady(() => {
this.drawImage()
})
.onAreaChange((value: Area, newVal: Area) => {
this.initPosition.x = Math.round(newVal.position.x as number)
this.initPosition.y = Math.round(newVal.position.y as number)
})
将从相册选择的图片进行了绘制,可以正常显示,并且在图片的上面我又绘制了裁剪框可以正常移动。使用下面代码进行了裁剪:
async clipImage() {
let x = this.clipRect.x - this.initPosition.x;
let y = this.clipRect.y - this.initPosition.y;
Logger.info('Safe', ':------裁剪x '+ x +'裁剪y:'+y);
Logger.info('Safe', ':------height= '+vp2px(this.clipRect.height) +'width =:'+vp2px(this.clipRect.width));
await this.pixelMap?.crop({
x: vp2px(x),
y: vp2px(y),
size: { height: vp2px(this.clipRect.height), width: vp2px(this.clipRect.width)}
})
this.cropImageInfo = await this.pixelMap?.getImageInfo();
this.isCrop = true
//this.rotateOn = true
//将裁剪完的图片进行存储
if (this.pixelMap) {
Logger.info('Safe', ':------存储pixelMap ' );
GlobalContextCamera.get().setObject('clipImagePixelMap', this.pixelMap);
}
}
为什么裁剪的内容始终是左上角一部分内容,即使我的框选框已经框选了全部,裁剪的内容依然是左上角一部分内容。 实际需求是在图片上面绘制了一个9宫格,可以上下左右移动9宫格,点击裁剪,裁剪9宫格所选中的区域的内容为新图片
更多关于HarmonyOS 鸿蒙Next image.PixelMap裁剪始终裁剪不到框选的位置的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
这边写一个demo,可以参考修改一下,剪裁框只实现了左上和右下部分
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { image } from '@kit.ImageKit';
import { picker } from '@kit.CoreFileKit';
import { fileIo } from '@kit.CoreFileKit';
@Entry
@Component
struct WebComponent {
@State pixelMap: PixelMap | undefined = undefined;
@State pixelMap1: PixelMap | undefined = undefined;
private settings: RenderingContextSettings = new RenderingContextSettings(true)
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
private settings1: RenderingContextSettings = new RenderingContextSettings(true)
private context1: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings1)
getPicture() {
try {
let uris: Array<string> = [];
let PhotoSelectOptions = new picker.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 1;
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
uris = PhotoSelectResult.photoUris;
let file = fileIo.openSync(uris[0], fileIo.OpenMode.READ_ONLY);
console.info('file fd: ' + file.fd);
let buffer = new ArrayBuffer(4096);
let readLen = fileIo.readSync(file.fd, buffer);
console.info('readSync data to file succeed and buffer size is:' + readLen);
const imageSource: image.ImageSource = image.createImageSource(file.fd);
let decodingOptions: image.DecodingOptions = {
editable: true,
desiredPixelFormat: 3,
}
imageSource.createPixelMap(decodingOptions, (err: BusinessError, pixelMap: image.PixelMap) => {
if (err !== undefined) {
console.error(`Failed to create pixelMap.code is ${err.code},message is ${err.message}`);
} else {
this.pixelMap = pixelMap
this.context.drawImage(this.pixelMap,0,0,350,350)
console.info('Succeeded in creating pixelMap object.');
}
})
}).catch((err: BusinessError) => {
console.error(`Invoke photoPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error('photoPicker failed with err: ' + JSON.stringify(err));
}
}
@State xLocation:number = 350
@State yLocation:number = 350
@State leftTop:number = 0
@State RightTop:number = 0
@State leftBottom:number = 350
@State RightBottom:number = 350
build() {
Column() {
Stack(){
Canvas(this.context)
Canvas(this.context1)
.onReady(()=>{
this.context1.lineWidth = 6
this.context1.strokeRect(10, 10, 340, 340)
})
.onTouch(event => {
let x = event.touches[0].screenX
let y = event.touches[0].screenY
console.log('移动时leftTop '+this.leftTop)
console.log('移动时RightTop '+this.RightTop)
console.log('移动时leftBottom '+this.leftBottom)
console.log('移动时RightBottom '+this.RightBottom)
if (x>180&&y>180) {
this.xLocation = x
this.yLocation = y
this.context1.reset()
this.context1.lineWidth = 6
this.context1.strokeRect(0, 0, this.xLocation, this.yLocation)
this.leftTop = 0
this.RightTop = 0
this.leftBottom = this.xLocation
this.RightBottom = this.yLocation
}
else if (x<180&&y<180){
this.context1.reset()
this.context1.lineWidth = 6
this.leftTop = x
this.RightTop = y
this.leftBottom = this.xLocation
this.RightBottom = this.yLocation
this.context1.strokeRect(x, y, this.xLocation-x, this.yLocation-y)
}
})
}
.height('80%')
Row(){
Button('选择图片').onClick(() => {
this.getPicture()
})
.height(50)
Button('剪裁').onClick(()=>{
let pixelmap = this.context.getPixelMap(0, 0, 350, 350)
if (this.pixelMap != undefined) {
let height1 = Math.round(this.leftBottom-this.RightTop)
let width1 = Math.round(this.RightBottom-this.leftTop)
let region: image.Region = { x: vp2px(Math.round(this.leftTop)), y: vp2px(Math.round(this.RightTop)-1), size: { height: vp2px(height1), width: vp2px(width1) } };
if (pixelmap != undefined) {
pixelmap.crop(region, (err: BusinessError) => {
if (err) {
console.error(`Failed to scale pixelmap. code is ${err.code}, message is ${err.message}`);
return;
} else {
this.pixelMap1 = pixelmap
this.context.drawImage(this.pixelMap1,0,0,350,350)
console.info("Succeeded in scaling pixelmap.");
}
})
}
}
})
}
.width('100%')
.height('100%')
}
}
剪裁之前需要通过canvas重新获取一次pixelMap: let pixelmap = this.context.getPixelMap(0, 0, 350, 350),使用此pixelMap来进行剪裁操作
更多关于HarmonyOS 鸿蒙Next image.PixelMap裁剪始终裁剪不到框选的位置的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对帖子标题“HarmonyOS 鸿蒙Next image.PixelMap裁剪始终裁剪不到框选的位置”的问题,可能的原因及解决方法如下:
在HarmonyOS中,当你使用PixelMap
进行裁剪操作时,如果裁剪结果始终不符合预期,可能是因为裁剪区域的坐标或尺寸设置不正确。首先,请确保你设置的裁剪区域(通常是一个Rect
对象)完全位于PixelMap
的范围内。裁剪区域的坐标和尺寸应该与PixelMap
的实际尺寸相匹配,且不能超出其边界。
其次,检查裁剪操作的代码逻辑,确保在裁剪前没有误修改裁剪区域的参数。例如,如果你在裁剪前对裁剪区域进行了缩放、平移或其他变换,可能会导致裁剪位置不准确。
此外,还需要注意PixelMap
的像素格式和颜色空间,因为不同的像素格式和颜色空间可能会影响裁剪结果的显示。
如果以上步骤都确认无误,但问题依旧存在,可能是PixelMap
裁剪功能的实现存在bug或限制。此时,你可以尝试更新HarmonyOS SDK到最新版本,或者查阅HarmonyOS的官方文档和社区论坛,看看是否有其他开发者遇到并解决了类似的问题。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html