国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

uni-app 調(diào)用相機(jī)或相冊圖片并轉(zhuǎn)為base64格式上傳圖片

這篇具有很好參考價值的文章主要介紹了uni-app 調(diào)用相機(jī)或相冊圖片并轉(zhuǎn)為base64格式上傳圖片。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

1、調(diào)用相機(jī)或相冊上傳圖片

uni.chooseImage({
  	count: 1, // 最多可以選擇的圖片張數(shù)
    sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有
    sourceType: ['camera','album'], // camera調(diào)用相機(jī)拍照,album是打開手機(jī)相冊
    success: (res)=> {
		console.log(JSON.stringify(res.tempFilePaths));
    }
})

2、圖片文件轉(zhuǎn)base64

(1)下載插件

npm i image-tools --save

(2)頁面引入插件 文章來源地址http://www.zghlxwxcb.cn/news/detail-511601.html

<template>
	<view class="container">
		<view class="upload">
			<view class="img-box" v-if="form.visitorPicture">
				<image :src="form.visitorPicture" @click="handlePreview" mode="aspectFill"></image>
				<uni-icons type="clear" class="clear" @click="form.visitorPicture = ''"></uni-icons>
			</view>
			<uni-icons type="plusempty" class="upload-box" @click="handleUpload" v-else></uni-icons>
		</view>
		<uni-icons type="right"></uni-icons>
	</view>
</template>

<script>
	import MyToast from './components/myToast.vue'
	export default {
		name: 'info',
		data() {
			return {
				form: {
					visitorPicture: ""
				}
			}
		},
		methods: {
			// 上傳頭像
			handleUpload() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['camera', 'album'],
					success: (res) => {
						uni.showLoading({
							title: '圖片上傳中'
						});
						if(res.tempFilePaths[0].split(".")[1] === "jpg" || res.tempFilePaths[0].split(".")[1] === "JPG") {
							if(res.tempFiles[0].size <= 10 * 1024 * 1024) {
								// 圖片轉(zhuǎn)為base64
								pathToBase64(res.tempFilePaths[0]).then(path => {
									this.getImageUrl(path);
								}).catch(error => {
									uni.hideLoading();
								})
							} else {
								uni.hideLoading();
								this.errorToast = "請上傳小于10MB的圖片";
								this.$refs.myToast.show();
							}
						} else {
							uni.hideLoading();
							this.errorToast = "請上傳jpg格式的圖片";
							this.$refs.myToast.show();
						}
					}
				});
			},
			// 獲取上傳到服務(wù)器圖片的在線地址
			getImageUrl(path) {
				// todo 調(diào)用接口上傳base64圖片到后端
			},
			// 圖片預(yù)覽
			handlePreview() {
				uni.previewImage({
					current: 0,
					urls: [this.form.visitorPicture]
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.upload {
		width: 100rpx;
		height: 100rpx;
		position: relative;
		.upload-box {
			width: 100% !important;
			height: 100%;
			margin-left: 0 !important;
			justify-content: center;
			border: 2rpx #DDDDDD solid;
			border-radius: 4rpx;
			box-sizing: border-box;
			position: absolute;
			top: 0;
			left: 0;
			/deep/ .uniui-plusempty {
				width: 100%;
				height: 100%;
				line-height: 96rpx;
				text-align: center;
				font-size: 40rpx !important;
				color: #CCCCCC !important;
			}
		}
		.img-box {
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			image {
				width: 100%;
				height: 100%;
				border-radius: 4rpx;
			}
			.clear {
				width: 32rpx;
				height: 32rpx;
				position: absolute;
				right: 0;
				top: 0;
				/deep/ .uniui-clear {
					color: #7F7F7F !important;
					font-size: 32rpx !important;
				}
			}
		}
	}
</style>

3、image-tools/index.js源碼

function dataUrlToBase64(str) {
    var array = str.split(',')
    return array[array.length - 1]
}

var index = 0
function getNewFileId() {
    return Date.now() + String(index++)
}

function biggerThan(v1, v2) {
    var v1Array = v1.split('.')
    var v2Array = v2.split('.')
    var update = false
    for (var index = 0; index < v2Array.length; index++) {
        var diff = v1Array[index] - v2Array[index]
        if (diff !== 0) {
            update = diff > 0
            break
        }
    }
    return update
}

export function pathToBase64(path) {
    return new Promise(function(resolve, reject) {
        if (typeof window === 'object' && 'document' in window) {
            if (typeof FileReader === 'function') {
                var xhr = new XMLHttpRequest()
                xhr.open('GET', path, true)
                xhr.responseType = 'blob'
                xhr.onload = function() {
                    if (this.status === 200) {
                        let fileReader = new FileReader()
                        fileReader.onload = function(e) {
                            resolve(e.target.result)
                        }
                        fileReader.onerror = reject
                        fileReader.readAsDataURL(this.response)
                    }
                }
                xhr.onerror = reject
                xhr.send()
                return
            }
            var canvas = document.createElement('canvas')
            var c2x = canvas.getContext('2d')
            var img = new Image
            img.onload = function() {
                canvas.width = img.width
                canvas.height = img.height
                c2x.drawImage(img, 0, 0)
                resolve(canvas.toDataURL())
                canvas.height = canvas.width = 0
            }
            img.onerror = reject
            img.src = path
            return
        }
        if (typeof plus === 'object') {
            plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
                entry.file(function(file) {
                    var fileReader = new plus.io.FileReader()
                    fileReader.onload = function(data) {
                        resolve(data.target.result)
                    }
                    fileReader.onerror = function(error) {
                        reject(error)
                    }
                    fileReader.readAsDataURL(file)
                }, function(error) {
                    reject(error)
                })
            }, function(error) {
                reject(error)
            })
            return
        }
        if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
            wx.getFileSystemManager().readFile({
                filePath: path,
                encoding: 'base64',
                success: function(res) {
                    resolve('data:image/png;base64,' + res.data)
                },
                fail: function(error) {
                    reject(error)
                }
            })
            return
        }
        reject(new Error('not support'))
    })
}

export function base64ToPath(base64) {
    return new Promise(function(resolve, reject) {
        if (typeof window === 'object' && 'document' in window) {
            base64 = base64.split(',')
            var type = base64[0].match(/:(.*?);/)[1]
            var str = atob(base64[1])
            var n = str.length
            var array = new Uint8Array(n)
            while (n--) {
                array[n] = str.charCodeAt(n)
            }
            return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))
        }
        var extName = base64.split(',')[0].match(/data\:\S+\/(\S+);/)
        if (extName) {
            extName = extName[1]
        } else {
            reject(new Error('base64 error'))
        }
        var fileName = getNewFileId() + '.' + extName
        if (typeof plus === 'object') {
            var basePath = '_doc'
            var dirPath = 'uniapp_temp'
            var filePath = basePath + '/' + dirPath + '/' + fileName
            if (!biggerThan(plus.os.name === 'Android' ? '1.9.9.80627' : '1.9.9.80472', plus.runtime.innerVersion)) {
                plus.io.resolveLocalFileSystemURL(basePath, function(entry) {
                    entry.getDirectory(dirPath, {
                        create: true,
                        exclusive: false,
                    }, function(entry) {
                        entry.getFile(fileName, {
                            create: true,
                            exclusive: false,
                        }, function(entry) {
                            entry.createWriter(function(writer) {
                                writer.onwrite = function() {
                                    resolve(filePath)
                                }
                                writer.onerror = reject
                                writer.seek(0)
                                writer.writeAsBinary(dataUrlToBase64(base64))
                            }, reject)
                        }, reject)
                    }, reject)
                }, reject)
                return
            }
            var bitmap = new plus.nativeObj.Bitmap(fileName)
            bitmap.loadBase64Data(base64, function() {
                bitmap.save(filePath, {}, function() {
                    bitmap.clear()
                    resolve(filePath)
                }, function(error) {
                    bitmap.clear()
                    reject(error)
                })
            }, function(error) {
                bitmap.clear()
                reject(error)
            })
            return
        }
        if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
            var filePath = wx.env.USER_DATA_PATH + '/' + fileName
            wx.getFileSystemManager().writeFile({
                filePath: filePath,
                data: dataUrlToBase64(base64),
                encoding: 'base64',
                success: function() {
                    resolve(filePath)
                },
                fail: function(error) {
                    reject(error)
                }
            })
            return
        }
        reject(new Error('not support'))
    })
}

到了這里,關(guān)于uni-app 調(diào)用相機(jī)或相冊圖片并轉(zhuǎn)為base64格式上傳圖片的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • uni-app 支持 app端, h5端,微信小程序端 圖片轉(zhuǎn)換文件格式 和 base64

    uni-app 支持 app端 h5端,微信小程序端 圖片轉(zhuǎn)換文件格式 和 base64,下方是插件市場的地址 app端 h5端,微信小程序端 圖片轉(zhuǎn)換文件格式 和 base64 - DCloud 插件市場 https://ext.dcloud.net.cn/plugin?id=13926

    2024年02月13日
    瀏覽(91)
  • uni-app 經(jīng)驗分享,從入門到離職(實戰(zhàn)篇)——模擬從后臺獲取圖片路徑數(shù)據(jù)后授權(quán)相冊以及保存圖片到本地(手機(jī)相冊)

    uni-app 經(jīng)驗分享,從入門到離職(實戰(zhàn)篇)——模擬從后臺獲取圖片路徑數(shù)據(jù)后授權(quán)相冊以及保存圖片到本地(手機(jī)相冊)

    這篇文章是本專欄 uni-app 的項目實戰(zhàn)篇,主要內(nèi)容的是模擬前端通過調(diào)用接口,然后獲取到數(shù)據(jù)圖片的路徑數(shù)據(jù),然后授權(quán)相冊,最后把圖片保存到本地(相冊)。 本專欄主要是分享和介紹從零到一學(xué)習(xí)和使用的 uni-app 的筆記和個人經(jīng)驗。通過個人的學(xué)習(xí)經(jīng)驗和工作經(jīng)驗來給

    2024年02月08日
    瀏覽(27)
  • 【uni-app】后端返回base64轉(zhuǎn)二維碼并顯示在canvas生成海報

    【uni-app】后端返回base64轉(zhuǎn)二維碼并顯示在canvas生成海報

    使用官方的 uni.getFileSystemManager().writeFile() 方法可將base64碼轉(zhuǎn)成的二維碼顯示在畫布上,代碼如下: const obj = { ?? ??? ??? ??? ??? ?page: \\\'pages/sort/goodsDetail\\\', ?? ??? ??? ??? ??? ?co_Nu: this.goodInfo.co_Nu ?? ??? ??? ??? ?} ?? ??? ??? ??? ?const _this = this ?? ???

    2024年02月11日
    瀏覽(79)
  • uni-app uni-file-picker文件上傳實現(xiàn)拍攝從相冊選擇獲取圖片上傳文檔服務(wù)器(H5上傳-微信小程序上傳)

    uni-app uni-file-picker文件上傳實現(xiàn)拍攝從相冊選擇獲取圖片上傳文檔服務(wù)器(H5上傳-微信小程序上傳)

    前言 最近在使用uni-app寫H5移動端,有一個從手機(jī)拍攝從相冊選擇獲取圖片上傳到文檔服務(wù)器功能。 查閱uni-app發(fā)現(xiàn)關(guān)于上傳圖片,uni-file-picker文件上傳,uni.chooseImage,uni.uploadFile H5上傳時它和pc端原理差不多,都是file對象上傳,PC端是通過new file對象,uni-app是直接提供了 微信

    2024年02月15日
    瀏覽(95)
  • flutter base64圖片保存到相冊

    首先base64轉(zhuǎn)成uint8List,然后再用插件保存到相冊(沒有內(nèi)置的方法處理) 保存圖片的插件 完整代碼如下 為啥要用下面 因為’data:image/png;base64,’ is part of the data URL,不是base-64字符串的一部分。您需要首先從URL中提取base-64數(shù)據(jù)。 否則就會報錯如下: 網(wǎng)站用圖片轉(zhuǎn)base64如下

    2024年02月01日
    瀏覽(17)
  • uni-app 微信小程序 支付寶小程序(alipay) 百度小程序(baidu),預(yù)覽pdf(鏈接和base64) 及下載(僅微信),window.open uni.downloadFile

    uni-app 微信小程序 支付寶小程序(alipay) 百度小程序(baidu),預(yù)覽pdf(鏈接和base64) 及下載(僅微信),window.open uni.downloadFile

    廢話不多說直接上代碼吧 之前搜了一大堆有的沒的,最終還是小伙伴巴拉文檔一起找到的方案(離不開小伙伴的幫助,自己總?cè)菀紫萑胨谰?,在此鳴謝 疾風(fēng)李青?。?想起個事:一定要給這些路徑的域名配到相應(yīng)的開發(fā)管理上,其他平臺不過多贅述了 首先是預(yù)覽,由于我這

    2024年02月15日
    瀏覽(94)
  • 【base64】JavaScript&uniapp 將圖片轉(zhuǎn)為base64并展示

    【base64】JavaScript&uniapp 將圖片轉(zhuǎn)為base64并展示

    Base64是一種用于編碼二進(jìn)制數(shù)據(jù)的方法,它將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為文本字符串。它的主要目的是在網(wǎng)絡(luò)傳輸或存儲過程中,通過將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為可打印字符的形式進(jìn)行傳輸 ?圖片大小從1.36MB到169kb 上面的代碼中,toDataURL產(chǎn)生的是圖片的base64編碼,Base64編碼必須是完整且正確

    2024年02月11日
    瀏覽(26)
  • 【base64碼轉(zhuǎn)為圖片,并預(yù)覽】

    【base64碼轉(zhuǎn)為圖片,并預(yù)覽】

    開發(fā)工具及需求介紹 (1)開發(fā)工具:HBuilder (2)數(shù)據(jù)庫:SQLite (3)組件庫:uni-app (4)需求:(后端)將圖片信息加密,前端接收到的是一個base64碼。前端需要利用這些信息,轉(zhuǎn)成 image src=\\\"imageURL\\\"/image 中的imageURL,以便能顯示出圖片。后端返回的信息,我們必需要的是”圖

    2024年02月05日
    瀏覽(18)
  • uni-app 獲取android相冊

    在uni-app中提供的封裝好的api中沒有提供獲取手機(jī)相冊的能力,只能打開相冊后由用戶選擇其中的照片,而插件庫中提供的獲取相冊的插件都是收費(fèi)的,這里為大家分享一個可以自己獲取android相冊的代碼段:

    2024年02月11日
    瀏覽(22)
  • 解決uni-app開發(fā)小程序時,CSS調(diào)用本地圖片當(dāng)背景時不能使用的問題

    解決uni-app開發(fā)小程序時,CSS調(diào)用本地圖片當(dāng)背景時不能使用的問題

    uniapp官方給出的解釋就是小程序不支持本地圖片,只支持網(wǎng)絡(luò)訪問或者base64。 當(dāng)背景圖片小于40kb的時候還好,uniapp會自動轉(zhuǎn)為base64格式;但是大于40kb時候就不行了,目前我了解的有三種方式解決: 1.可以通過動態(tài)樣式“:style”來解決,在標(biāo)簽上如下編寫: 接著在data里聲明

    2024年02月12日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包