头像剪裁/图片上传案例

 注意:请勿复制源码,需要源码的可以发邮件至2445478193@qq.com

<!DOCTYPE>
<html lang="en">
<head>
	<title>用户头像剪裁</title>
	<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
	<link rel="stylesheet" href="jquery.Jcrop.css">
	<script src="jquery-1.12.4.js"></script>
	<script src="jquery.Jcrop.js"></script>
	<script src="ajaxfileupload.js"></script>
	<link rel="stylesheet" href="main.css" type="text/css" />
	<link rel="stylesheet" href="demos.css" type="text/css" />
	<style type="text/css">

/* Apply these styles only when #preview-pane has
   been placed within the Jcrop widget */
.jcrop-holder #preview-pane {
  display: block;
  position: absolute;
  z-index: 2000;
  top: 10px;
  right: -450px;
  padding: 6px;
  border: 1px rgba(0,0,0,.4) solid;
  background-color: white;

  -webkit-border-radius: 6px;
  -moz-border-radius: 6px;
  border-radius: 6px;

  -webkit-box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2);
  -moz-box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2);
  box-shadow: 1px 1px 5px 2px rgba(0, 0, 0, 0.2);
}

/* The Javascript code will set the aspect ratio of the crop
   area based on the size of the thumbnail preview,
   specified here */
#preview-pane .preview-container {
  width: 250px;
  height: 170px;
  overflow: hidden;
}

</style>
</head>
<body>
<div class="container">
	<div class="row">
		<div class="span12">
			<div class="jc-demo-box">
				<div class="page-header">
					<h1>头像剪裁</h1>
				</div>
				<img src="images/test.jpeg" id="target" />
				<div id="preview-pane" >
					<div class="preview-container">
						<img src="images/test.jpeg" class="jcrop-preview" style="width: 450px,height:auto;" />
					</div >
					<div style='float:left;display:inline;'>
						<a class='btn_addPic' href="javascript:void(0);">
							<span><EM>+</EM>添加图片</span> 
							<input id="upload_image" type="file" name="upimage" accept="image/*" class = "filePrew"/>
						</a>
					</div>
					<div style='float:right;display:inline;'>
						<a class='btn_addPic' href="javascript:void(0);" onclick='submit()'>
							<span>提交</span> 
						</a>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
</body>
<script type="text/javascript">
	var global_api = "";
	var boundx ="";//页面图片宽度
	var boundy ="";//页面图片高度
	var tag = false;
	$(function() {

		// Create variables (in this scope) to hold the API and image size
		//创建变量(在这个范围)的API和图像大小
		var jcrop_api,

		// Grab some information about the preview pane
		//获取一些信息预览窗格
		$preview = $('#preview-pane'),
		$pcnt = $('#preview-pane .preview-container'),
		$pimg = $('#preview-pane .preview-container img'),
		xsize = $pcnt.width(),
		ysize = $pcnt.height();

		$('#target').Jcrop({
			boxWidth:200,
      		boxHeight:200,
			onChange: updatePreview,
			onSelect: updatePreview,
			aspectRatio: xsize / ysize
			},function(){
			// Use the API to get the real image size
			//使用API来获得真实的图像大小
			var bounds = this.getBounds();
				boundx = bounds[0];
				boundy = bounds[1];
			// Store the API in the jcrop_api variable
			//jcrop_api变量中存储API
			jcrop_api = this;
			// Move the preview into the jcrop container for css positioning
			//预览进入jcrop容器css定位
			$preview.appendTo(jcrop_api.ui.holder);
		});
		function updatePreview(c){
			if (parseInt(c.w) > 0)
			global_api=c;
			{
				var rx = xsize / c.w;
				var ry = ysize / c.h;
				$pimg.css({
					width: Math.round(rx * boundx) + 'px',
					height: Math.round(ry * boundy) + 'px',
					marginLeft: '-' + Math.round(rx * c.x) + 'px',
					marginTop: '-' + Math.round(ry * c.y) + 'px'
				});
			}
		};
		//=======选择图片回显===============
		$('#upload_image').change(function(event) {
		// 根据这个 <input> 获取文件的 HTML5 js 对象
			var files = event.target.files, file; 
			if (files && files.length > 0) {
				// 获取目前上传的文件
				file = files[0];
				// 下面是关键的关键,通过这个 file 对象生成一个可用的图像 URL
				// 获取 window 的 URL 工具
				var URL = window.URL || window.webkitURL;
				// 通过 file 生成目标 url
				var imgURL = URL.createObjectURL(file);
				// 用这个 URL 产生一个 <img> 将其显示出来
				$('.jcrop-holder img').attr('src', imgURL);
					$('.preview-container img').attr('src', imgURL);
				}
		});
		//=============是否选择了本地图片==================
		$("#upload_image").change(function(){
			tag=true;
		});
	});
	//========================================================
	//======图片保存===========
	function submit(){
		if(tag&&global_api != ""){
			ajaxFileUpload();
		}else{
			alert('你是不是什么事情都没干?');
		}
	}
	// ajax文件上传
	function ajaxFileUpload() {
		$.ajaxFileUpload({
			url: 'uploadphoto', //用于文件上传的服务器端请求地址
			secureuri: false, //一般设置为false
			fileElementId: 'upload_image', //文件上传空间的id属性
			dataType: 'json', //返回值类型 一般设置为json
			data:{x:global_api.x,y:global_api.y,w:global_api.w,h:global_api.h,pw:boundx,ph:boundy,unid:'test'}, //一同上传的数据 
			success: function (data){ //服务器成功响应处理函数
				if(data.result){
					alert('成功');
				}else{
					alert('失败');
				}
				window.location.reload();//刷新当前页面
			}
		});
	}

</script>
</html>

 所需文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现在Android中上传头像并进行剪裁,可以使用以下步骤: 1. 添加依赖项:在app的build.gradle文件中添加以下依赖项: ``` implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.+' ``` 2. 创建一个ImageView,用于显示用户选择的图片。 3. 创建一个Button,用于触发选择图片的操作。 4. 在Button的onClick方法中,打开图片选择器,让用户选择需要上传的图片。可以使用以下代码: ```java Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, REQUEST_IMAGE_PICKER); ``` 其中,REQUEST_IMAGE_PICKER是一个自定义的int值,用于标识选择器返回的结果。 5. 在Activity的onActivityResult方法中,获取用户选择的图片,并打开剪裁界面。可以使用以下代码: ```java if (requestCode == REQUEST_IMAGE_PICKER && resultCode == RESULT_OK) { Uri imageUri = data.getData(); CropImage.activity(imageUri) .setGuidelines(CropImageView.Guidelines.ON) .setCropShape(CropImageView.CropShape.OVAL) .setFixAspectRatio(true) .start(this); } ``` 其中,CropImage是从依赖项中引入的类,通过调用.activity方法打开剪裁界面。setGuidelines方法用于显示剪裁框,setCropShape方法设置剪裁框的形状,setFixAspectRatio方法设置剪裁框的宽高比。 6. 在Activity的onActivityResult方法中,获取剪裁后的图片,并显示在ImageView中。可以使用以下代码: ```java if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { CropImage.ActivityResult result = CropImage.getActivityResult(data); if (resultCode == RESULT_OK) { Uri croppedImageUri = result.getUri(); imageView.setImageURI(croppedImageUri); } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { Exception error = result.getError(); // Handle crop error } } ``` 以上就是在Android中上传头像并进行剪裁的步骤,其中使用了开源库android-image-cropper来实现剪裁功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值