1.创建组件components > uploadImg > index.vue

<template>
<input type="file" name="pic" ref="file" accept="image/*" @change="upload"/>
</template> <script>
import Exif from "exif-js";
import { upload } from "@/api"; export default {
props: {
},
data() {
return {
uploadName: ""
};
},
mounted() {
console.log(this.uploadType);
},
methods: {
upload(e) {
if (e.target.files.length > 0) {
this.$vux.loading.show("Loading...");
let files = e.target.files || e.dataTransfer.files;
if (!files.length) return;
this.picValue = files[0]
this.uploadName = files[0].name; // 上传图片
var u = navigator.userAgent, app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if (isAndroid) {
this.axiosFun(this.picValue);
}
if (isIOS) {
   this.imgPreview(this.picValue);
} // 上传视频
// this.axiosFun(this.picValue);
}
},
imgPreview(file) {
let self = this;
let Orientation;
//去获取拍照时的信息,解决拍出来的照片旋转问题
Exif.getData(file, function() {
Orientation = Exif.getTag(this, "Orientation");
});
// 看支持不支持FileReader
if (!file || !window.FileReader) return; if (/^image/.test(file.type)) {
// 创建一个reader
let reader = new FileReader(); // 将图片2将转成 base64 格式
reader.readAsDataURL(file); // 读取成功后的回调
reader.onloadend = function() {
let result = this.result;
let img = new Image();
img.src = result;
//判断图片是否大于100K,是就直接上传,反之压缩图片
if (this.result.length <= 100 * 1024) {
self.headerImage = this.result;
self.postImg();
} else {
img.onload = function() {
let data = self.compress(img, Orientation);
self.headerImage = data;
self.postImg();
};
}
};
}
},
compress(img, Orientation) {
let _this = this
let canvas = document.createElement("canvas");
let ctx = canvas.getContext("2d");
//瓦片canvas
let tCanvas = document.createElement("canvas");
let tctx = tCanvas.getContext("2d");
let initSize = img.src.length;
let width = img.width;
let height = img.height;
//如果图片大于四百万像素,计算压缩比并将大小压至400万以下
let ratio;
if ((ratio = width * height / 4000000) > 1) {
console.log("大于400万像素");
ratio = Math.sqrt(ratio);
width /= ratio;
height /= ratio;
} else {
ratio = 1;
}
canvas.width = width;
canvas.height = height;
// 铺底色
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
//如果图片像素大于100万则使用瓦片绘制
let count;
if ((count = width * height / 1000000) > 1) {
console.log("超过100W像素");
count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
// 计算每块瓦片的宽和高
let nw = ~~(width / count);
let nh = ~~(height / count);
tCanvas.width = nw;
tCanvas.height = nh;
for (let i = 0; i < count; i++) {
for (let j = 0; j < count; j++) {
tctx.drawImage(
img,
i * nw * ratio,
j * nh * ratio,
nw * ratio,
nh * ratio,
0,
0,
nw,
nh
);
ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
}
}
} else {
ctx.drawImage(img, 0, 0, width, height);
}
//修复ios上传图片的时候 被旋转的问题
if (Orientation != "" && Orientation != 1) {
switch (Orientation) {
case 6: //需要顺时针(向左)90度旋转
_this.rotateImg(img, "left", canvas);
break;
case 8: //需要逆时针(向右)90度旋转
_this.rotateImg(img, "right", canvas);
break;
case 3: //需要180度旋转
_this.rotateImg(img, "right2", canvas);
break;
}
}
//进行最小压缩
let ndata = canvas.toDataURL("image/jpeg", 0.1);
tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
return ndata;
},
rotateImg(img, direction, canvas) {
//最小与最大旋转方向,图片旋转4次后回到原方向
const min_step = 0;
const max_step = 3;
if (img == null) return;
//img的高度和宽度不能在img元素隐藏后获取,否则会出错
let height = img.height;
let width = img.width;
let step = 2;
if (step == null) {
step = min_step;
}
if (direction == "right") {
step++;
//旋转到原位置,即超过最大值
step > max_step && (step = min_step);
} else if (direction == "right2") {
step = 2;
} else {
step--;
step < min_step && (step = max_step)
}
//旋转角度以弧度值为参数
let degree = step * 90 * Math.PI / 180;
let ctx = canvas.getContext("2d");
switch (step) {
case 0:
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0);
break;
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, 0, -height);
break;
case 2:
canvas.width = width;
canvas.height = height;
ctx.rotate(degree);
ctx.drawImage(img, -width, -height);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, -width, 0);
break;
}
},
postImg() {
//这里写接口
// console.log(this.headerImage)
const block = this.headerImage.split(";"); const contentType = block[0].split(":")[1]; // In this case "image/jpeg" const realData = block[1].split(",")[1]; // In this case "R0lGODlhPQBEAPeoAJosM...." var blob = this.b64toBlob(realData, contentType);
this.axiosFun(blob);
},
uploadDefined(file, data, callback) {
let _this = this;
let gObjectName = data.dir + this.randomString() + this.getSuffix(this.uploadName);
let request = new FormData();
request.append("OSSAccessKeyId", data.accessid); // Bucket 拥有者的Access Key Id。
request.append("policy", data.policy); // policy规定了请求的表单域的合法性
request.append("Signature", data.signature); // 根据Access Key Secret和policy计算的签名信息,OSS验证该签名信息从而验证该Post请求的合法性
request.append("key", gObjectName); // 文件名字,可设置路径
request.append("success_action_status", "200"); // 让服务端返回200,不然,默认会返回204
request.append("x-oss-object-acl", "public-read");
request.append("file", file); let xhr = new XMLHttpRequest();
xhr.open("POST", data.host);
xhr.send(request);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// console.log("fileKey => " + data.host + "/" + gObjectName);
// console.log(_this.imgPaths);
_this.$vux.loading.hide();
callback(data.host, gObjectName);
} else if (xhr.status === 400){
_this.$vux.loading.hide();
_this.$vux.toast.text('图片太大,请换取小的图片')
}
};
},
async imgUpload(){
// const { data } = await uploadImg({url: host, })
},
async axiosFun(blob) {
let that = this;
const { data } = await upload({}); that.uploadDefined(blob, data, function(host, name) {
that.$emit("uploadImage", {
value: host + "/" + name,
});
});
},
randomString(len) {
len = len || 32;
var chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
var maxPos = chars.length;
var pwd = "";
for (var i = 0; i < len; i++) {
pwd += chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
},
getSuffix(filename) {
var pos = filename.lastIndexOf(".");
var suffix = "";
if (pos !== -1) {
suffix = filename.substring(pos);
}
return suffix;
},
b64toBlob(b64Data, contentType = "", sliceSize = 512) {
const byteCharacters = atob(b64Data);
const byteArrays = []; for (
let offset = 0;
offset < byteCharacters.length;
offset += sliceSize
) {
const slice = byteCharacters.slice(offset, offset + sliceSize); const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
} const byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray);
} const blob = new Blob(byteArrays, { type: contentType });
return blob;
}
}
};
</script> <style>
</style>

2.页面中引用组件

<script>
import uploadImg from '@/components/uploadImg' export default {
data () {
return {
imgList: [],
}
},
components: {
uploadImg
},
methods: {
uploadImage(val){
// 将上传的图片放入数组中
this.imgList.push(val.value)
},
}
</script>

vue 上传图片视频组件,可拍照选择照片,解决苹果手机拍照旋转问题的更多相关文章

  1. 解决微信小程序视频组件层级过高的问题

    本文首发于我的个人博客:http://www.fogcrane.org 前言 在微信小程序的开发中,总有一些"VIP"组件,他们的层级,高得让人抓狂,总是凌驾于很多其他低层级组件之 ...

  2. 后盾网lavarel视频项目---4、lavarel和vue都是{{}}表示变量,如何解决冲突

    后盾网lavarel视频项目---4.lavarel和vue都是{{}}表示变量,如何解决冲突 一.总结 一句话总结: @{{videos}}:@符号表示lavarel不处理:textarea nam ...

  3. Swift - 从相册中选择视频(过滤掉照片,使用UIImagePickerController)

    (本文代码已升级至Swift4) 有时我们需要从系统相册中选择视频录像,来进行编辑或者上传操作,这时使用 UIImagePickerController 就可以实现. 默认情况下,UIImagePic ...

  4. vue自定义可输入的选择框组件

    vue自定义可输入的选择框组件 props: 属性 说明 类型 默认值 selectDataList 下拉框中的内容 Array 空数组([]) value 输入框中的内容 String 空字符串(& ...

  5. 解决vue中element组件样式修改无效

    vue中element组件样式修改无效 <style> .detail{ .el-input__inner { height: 48px; } } </style> 直接写st ...

  6. NutUI 视频组件开发心得

    引子 说到在项目中引入一个视频,我们肯定会想到 HTML5 为我们提供的 Video 标签,它为我们提供了许多属性和方法,使用起来很方便,当然直接使用也会遇到各种兼容问题,在最初学习 Video 标签 ...

  7. vue学习之组件

    组件从注册方式分为全局组件和局部组件. 从功能类型又可以分为偏视图表现的(presentational)和偏逻辑的(动态生成dom),推荐在前者中使用模板,在后者中使用 JSX 或渲染函数动态生成组件 ...

  8. Vue 入门之组件化开发

    Vue 入门之组件化开发 组件其实就是一个拥有样式.动画.js 逻辑.HTML 结构的综合块.前端组件化确实让大的前端团队更高效的开发前端项目.而作为前端比较流行的框架之一,Vue 的组件和也做的非常 ...

  9. 使用vue与element组件

    1.安装element npm i element-ui -S 2.引入 在main.js写入一下内容 import Vue from 'vue'; import ElementUI from 'el ...

随机推荐

  1. [iptables] 如何用iptables管理桥接模式下的设备

    场景:qemu虚拟机通过tap设备与host的物理网卡通过bridge桥接上网. 如下: [root@host100 ~]# brctl show bridge name bridge id STP ...

  2. nessus无法访问https://localhost:8834/#/,解决方法。

    之前没弄明白为啥经常访问不了https://localhost:8834/#/,后面才发现是服务关闭了. 首先netstat -an 查看8834是否开启, 直接运行一下nessus目录下的nessu ...

  3. vsCode如何从github拉取项目

    最近刚使用vscode,有些操作还不太会,所以记录下. 1.开vscode使用CTRL+`或者点击查看到集成终端打开控制终端. 2.在终端中cd到存放文件夹下面,然后git clone https:/ ...

  4. Jmeter学习之-获取登录的oken值(1)

    ps: 这里只着重讲述如何实时获取其他接口返回的值,作为此次接口的参数传递,添加接口请求的相关不再详述,可查看上一篇文章 为了方便管理,此处将:登录接口单独放在一个线程组下面,需要使用登录接口返回的t ...

  5. Linux下安装Gensim

    依赖软件包:numpy 直接使用pip安装: [root@mycentos ~]#pip install gensim 安装gensim的时候会遇到下面的一系列错误: Cannot uninstall ...

  6. 使用new和newInstance()创建类的区别

    在初始化一个类,生成一个实例的时候,newInstance()方法和new关键字除了一个是方法,一个是关键字外,最主要有什么区别?它们的区别在于创建对象的方式不一样,前者是使用类加载机制,后者是创建一 ...

  7. iOS UI基础-21 WKWebView

    WKWebView,直接显示网页,需要引入第三方类:https://github.com/marcuswestin/WebViewJavascriptBridge 加上进度条代码,很完美使用 webV ...

  8. HTML5 元素属性介绍

    HTMLElement 表示所有的 HTML 元素. 这里将以事件属性和非事件属性的分类进行介绍. 事件属性大多继承自GlobalEventHandlers,非事件属性大多继承自Element. 菜单 ...

  9. SV class

    SV中的class通过new来创建实例,new函数可以传递多个参数: packet P1:  P1 = new:创建一个P1的对象: P2 = P1:P2也指向同一个对象: P2 = new P1:P ...

  10. vue使用桌面Element-UI和移动端MintUI的UI框架

    vue使用桌面Element-UI和移动端MintUI的UI框架 element-uiElement - 网站快速成型工具http://element-cn.eleme.io/#/zh-CN 安装:n ...