uniapp 将图片转成JPG格式

<template>

<view style="width: 100%; height: 100%;">

<image :src="imagePath" lazy-load style="width: 100%; height: 100%;"></image>

<canvas

style="background-color: red;"

:style="{width: target.width + 'px', height: target.height + 'px'}" canvas-id="target"></canvas>

</view>

</template>

<script>

export default {

data() {

return {

targetContext: null,

imagePath: '',

target: {

width: 300,

height: 300

}

}

},

onLoad(e) {

console.log(e);

// this.imagePath = e.imagepath;

this.targetContext = uni.createCanvasContext("target", this); //第一个画布

this.imageDetailMsg(e.imagepath);

},

methods: {

imageDetailMsg(imageUrl) {

let self = this;

uni.getImageInfo({

src: imageUrl,

success: function(imageInfo) {

console.log(imageInfo);

setTimeout(() => {

self.targetContext.drawImage(imageInfo.path, 0, 0, 300, 300, 0, 0, 300, 300);

self.targetContext.draw(false, () => {

uni.canvasToTempFilePath({

canvasId: "target",

fileType:'jpg',

success: (res) => {

var path = res.tempFilePath;

console.log('裁剪后的图片',res);

self.imagePath = path;

},

fail: (ev) => {

console.log(ev);

},

complete: () => {

}

}, self);

});

}, 100);

}

})

},

}

}

</script>

<style>

</style>

相关推荐
明远湖之鱼8 分钟前
从代码到像素:浏览器渲染原理与 React Fiber 架构的深度协作
前端·react.js·浏览器
Knight_AL26 分钟前
Spring Cloud Alibaba 项目中DataSource 配置失败?原来是 application.yml 和 bootstrap.yml 的坑
前端·bootstrap·html
怎么就重名了1 小时前
Kivy的属性系统
java·前端·数据库
hxjhnct2 小时前
JavaScript Promise 的常用API
开发语言·前端·javascript
web小白成长日记2 小时前
前端让我明显感受到了信息闭塞的恐怖......
前端·javascript·css·react.js·前端框架·html
GIS之路2 小时前
GDAL 实现创建几何对象
前端
liulilittle3 小时前
CLANG 交叉编译
linux·服务器·开发语言·前端·c++
自信阿杜3 小时前
跨标签页数据同步完全指南:如何选择最优通信方案
前端·javascript
牛马1113 小时前
WidgetsFlutterBinding.ensureInitialized()在 Flutter Web 端启动流程的影响
java·前端·flutter
Captaincc3 小时前
2025: The year in LLMs
前端·vibecoding