首页 技术 正文
技术 2022年11月12日
0 收藏 598 点赞 3,878 浏览 2843 个字

一、Html5实现头像上传和编辑

插件地址:

html5手机端裁剪图片上传头像代码

本地项目引入注意事项:

1.将html的js搬到外面的js文件中,便于管理

2.图片样式在html都是在页面写死,需要调整

3.页面引入css和js,editPic.js是页面提取出来的js

<link href="../js/fileupload/style.css" rel="external nofollow"  rel="stylesheet" type="text/css">
<script src="../js/fileupload/jquery.min.js" type="text/javascript"></script>
<script>window.jQuery|| document.write('<script src="js/jquery-2.1.1.min.js"><\/script>')</script>
<script src="../js/fileupload/iscroll-zoom.js"></script>
<script src="../js/fileupload/hammer.js"></script>
<script src="../js/fileupload/jquery.photoClip.js"></script>
<script src="../js/editPic.js" type="text/javascript"></script>

关键代码:

<!-- 上传图片的样式 -->
<article class="htmleaf-container" style="display: none;">
<div id="clipArea"
style="user-select: none; overflow: hidden; position: relative;">
<div class="photo-clip-view">
<div class="photo-clip-moveLayer">
<div class="photo-clip-rotateLayer"></div>
</div>
</div>
<div class="photo-clip-mask">
<div class="photo-clip-mask-left"></div>
<div class="photo-clip-mask-right"></div>
<div class="photo-clip-mask-top"></div>
<div class="photo-clip-mask-bottom"></div>
<div class="photo-clip-area"></div>
</div>
</div>
<div class="foot-use">
<div class="uploader1 blue">
<input type="button" name="file" class="button" value="打开">
<input id="file" type="file"
onchange="javascript:setImagePreview();" accept="image/*"
multiple="">
</div>
<button id="clipBtn">截取</button>
</div>
<div id="view"></div>
</article>

显示图片的位置

<p class="userPic mb10">
<a id="logox"><i><img id="show" src="" width="100%"></i></a>
</p>

修改$(“#clipBtn”)方法体

其中imgsource就是插件,剪切出来的base64位的图片编码,我们需要将编码转成图片保存

$("#clipBtn").click(
function() {
$.ajax({
type : 'POST',
url : PROJECT_PATH + '/upload/mobileUploadPic',
data : {
"imgsource" : imgsource,
"path" : "citizens"
},
dataType : 'text',
success : function(data) {
var ao = $.parseJSON(data);
if (ao.result) {
picFileSaveUrl = ao.obj.picFileSaveUrl;
$("#show").attr("src",PROJECT_PATH+picFileSaveUrl);
$("#pictureUrl").val(PROJECT_PATH+picFileSaveUrl);
$(".htmleaf-container").hide();
}},
// 调用出错执行的函数
error : function() {
}});})
});

二、Base64的存储为本地图片过程 

需要注意的是 图片的base64位是带有”data:image/jpeg;base64,”字段,需要去掉,才能保存图片的

public final static String BASE64_HEADER = "data:image/jpeg;base64,";// base64位的头部信息
File file = new File(picUrl);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}String base64ImgData = imgsource.substring(BASE64_HEADER.length(), imgsource.length() - 1);decodeBase64ToImage(base64ImgData, file);// 转成文件
/**
* 将Base64位编码的图片进行解码,并保存到指定目录
*
* @param base64
* base64编码的图片信息
* @return
*/
public static void decodeBase64ToImage(String base64, File file) {
BASE64Decoder decoder = new BASE64Decoder();
try {byte[] decoderBytes = decoder.decodeBuffer(base64);for (int i = 0; i < decoderBytes.length; ++i) {
// 调整异常数据
if (decoderBytes[i] < 0) {
decoderBytes[i] += 256;
}
}OutputStream write = new FileOutputStream(file);
write.write(decoderBytes);
write.flush();
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}

  完~

 

上一篇: Netbeans IDE配置
下一篇: SGU 158.Commuter Train
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922