jquery 获取上传图片的宽高和预览图

本文介绍了一种使用JavaScript实现文件上传后即时显示图片预览效果的方法,并提供了完整的代码示例。该方法通过读取文件对象的数据并转换为Data URL来展示上传的图片,同时获取图片的尺寸。

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>

<input name="pic" id="pic" class="txt_form" type="file"><span id="pic_info" style="color:red;"></span>
<img src="" alt="" id="lod_pic" width="580" >

  $('#pic').change(function(){
var file = this;
createReader(file.files[0], function (w, h, u) { 
// alert(w + ' '+ h +' '+u);
$('#lod_pic').attr("src",u);
$('#pic_info').text("(图片尺寸:"+w+"x"+h+")");
});
  })
createReader = function(file, whenReady) {
    var reader = new FileReader;
    reader.onload = function (evt) {
        var image = new Image();
        image.onload = function () {
        var pic_url = this.src;
            var width  = this.width;
            var height = this.height;
            if (whenReady) whenReady(width, height , pic_url);
        };
        image.src = evt.target.result;
    };
    reader.readAsDataURL(file);
}
在Vue项目中获取人像图片头像(包含脸头发)的及中心坐标,可按以下步骤实现: ### 1. 加载图片 首先,需要加载人像图片,可以使用`new Image()`来创建一个图片对象,并设置其`src`属性。这部分与获取普通图片的方式类似,可参考获取图片的方法,如创建实例对象并设置图片地址,等待图片加载完成后进行后续操作[^2][^3][^4]。 ```vue <template> <div> <img ref="image" :src="imageUrl" @load="onImageLoad" alt="人像图片"> </div> </template> <script> export default { data() { return { imageUrl: 'your-image-url.jpg', image: null }; }, methods: { onImageLoad() { this.image = this.$refs.image; // 图片加载完成后进行后续处理 this.detectHead(); }, detectHead() { // 此处进行头像检测坐标计算 } } }; </script> ``` ### 2. 进行头像检测 要获取头像的中心坐标,需要使用人脸检测库。在JavaScript中,常用的人脸检测库有`face-api.js`。以下是使用`face-api.js`进行人脸检测的示例: ```javascript import * as faceapi from 'face-api.js'; // 加载模型 async function loadModels() { await faceapi.nets.ssdMobilenetv1.loadFromUri('/models'); await faceapi.nets.faceLandmark68Net.loadFromUri('/models'); } // 检测人脸 async function detectFace(image) { const detections = await faceapi.detectSingleFace(image).withFaceLandmarks(); if (detections) { const landmarks = detections.landmarks; // 获取头像的边界框 const headBox = landmarks.getBoundingBox(); const width = headBox.width; const height = headBox.height; const centerX = headBox.x + width / 2; const centerY = headBox.y + height / 2; return { width, height, centerX, centerY }; } return null; } ``` ### 3. 整合代码 将上述步骤整合到Vue组件中: ```vue <template> <div> <img ref="image" :src="imageUrl" @load="onImageLoad" alt="人像图片"> </div> </template> <script> import * as faceapi from 'face-api.js'; export default { data() { return { imageUrl: 'your-image-url.jpg', image: null, headInfo: null }; }, async created() { await loadModels(); }, methods: { onImageLoad() { this.image = this.$refs.image; this.detectHead(); }, async detectHead() { const headInfo = await detectFace(this.image); if (headInfo) { this.headInfo = headInfo; console.log('头像:', headInfo.width); console.log('头像:', headInfo.height); console.log('头像中心坐标:', headInfo.centerX, headInfo.centerY); } } } }; async function loadModels() { await faceapi.nets.ssdMobilenetv1.loadFromUri('/models'); await faceapi.nets.faceLandmark68Net.loadFromUri('/models'); } async function detectFace(image) { const detections = await faceapi.detectSingleFace(image).withFaceLandmarks(); if (detections) { const landmarks = detections.landmarks; const headBox = landmarks.getBoundingBox(); const width = headBox.width; const height = headBox.height; const centerX = headBox.x + width / 2; const centerY = headBox.y + height / 2; return { width, height, centerX, centerY }; } return null; } </script> ``` ### 注意事项 - 确保将`face-api.js`的模型文件放置在正确的路径下(如`/models`)。 - 由于人脸检测是一个异步操作,需要使用`async/await`来处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值