改进后的显示照片(可以剪切,上传圆形的图片)

@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Photo.RESULT_IMAGE && data != null) {
			Cursor c = this.getContentResolver().query(data.getData(), null,
					null, null, null);
			c.moveToFirst();
			String imagePath = c.getString(c.getColumnIndex("_data"));
			c.close();
			File file = new File(imagePath);
			Uri originalUri = Uri.fromFile(file);
			ClipImage.startPhotoZoom(originalUri, REQUEST_CODE_CLIP,MainActivity.this);
		} else if (requestCode == Photo.RESULT_CAMERA) {
			File file = new File(Photo.TEMP_IMAGE_PATH);
			Uri uri_tack = Uri.fromFile(file);
			ClipImage.startPhotoZoom(uri_tack, REQUEST_CODE_CLIP,MainActivity.this);
		}else if(requestCode==REQUEST_CODE_CLIP){
			Bitmap photo = null;
			if (photo == null) {
				Bundle bundle = data.getExtras();
				if (bundle != null) {
					photo = (Bitmap) bundle.get("data");
				}
			}
			if (photo != null) {
				image.setImageBitmap(toRoundBitmap.toRoundBitmap(photo));
			}
		}
	}


剪切的工具类

public static void startPhotoZoom(Uri uri, int witch,Context context) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 300);
		intent.putExtra("outputY", 300);
		// 系统的裁剪图片默认对图片进行人脸识别,当识别到有人脸时,
		// 会按aspectX和aspectY为1来处理,如果想设置成自定义的裁剪比例,
		// 需要设置noFaceDetection为true。
		intent.putExtra("noFaceDetection", true);
		intent.putExtra("return-data", true);
		Activity a=(Activity)context;
		a.startActivityForResult(intent, witch);
	}

显示圆形图片的工具类

public class ToRoundBitmap {

	private static ToRoundBitmap toRoundBitmap;

	private ToRoundBitmap(Context context) {
	}

	public static ToRoundBitmap getInstance(Context context) {
		if (toRoundBitmap == null) {
			toRoundBitmap = new ToRoundBitmap(context);
		}

		return toRoundBitmap;
	}

	public Bitmap toRoundBitmap(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
		if (width <= height) {
			roundPx = width / 2;

			left = 0;
			top = 0;
			right = width;
			bottom = width;

			height = width;

			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		} else {
			roundPx = height / 2;

			float clip = (width - height) / 2;

			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;

			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}

		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final Paint paint = new Paint();
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);

		paint.setAntiAlias(true);// 设置画笔无锯齿

		canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas

		// 以下有两种方法画圆,drawRounRect和drawCircle
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
		// canvas.drawCircle(roundPx, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452

		canvas.drawBitmap(bitmap, src, dst, paint);// 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle

		return output;
	}
}



实现图片剪切上传并在服务器端显示图片,可以按照以下步骤进行: 1.在前端页面中,使用 JavaScript 插件实现图片剪切功能,比如 popular 的插件 jCrop。 2.在 Asp.net MVC 中,建立一个控制器,用于处理图片上传剪切。 3.在控制器中编写代码,接收前端页面中上传图片,并将其保存在服务器端。 4.使用 C# 的 System.Drawing 命名空间中的类,对保存在服务器端的图片进行剪切处理,并保存剪切后的图片。 5.最后,通过 ViewBag 将剪切后的图片路径传递给前端视图,在前端页面中显示剪切后的图片。 以下是一个简单的示例代码: 前端页面代码: ```html <div> <img id="preview" src="#" alt="选择图片" /> </div> <div> <input type="file" id="image" /> </div> <div> <button id="crop">剪切上传</button> </div> ``` JavaScript 代码: ```javascript $(function () { var jcrop_api; $('#image').change(function () { var file = this.files[0]; var reader = new FileReader(); reader.onload = function (e) { $('#preview').attr('src', e.target.result).Jcrop({ aspectRatio: 1, boxWidth: 500, boxHeight: 500, setSelect: [0, 0, 100, 100], onSelect: updateCoords, onChange: updateCoords }, function () { jcrop_api = this; }); } reader.readAsDataURL(file); }); function updateCoords(c) { $('#x').val(c.x); $('#y').val(c.y); $('#w').val(c.w); $('#h').val(c.h); } $('#crop').click(function () { var x = $('#x').val(); var y = $('#y').val(); var w = $('#w').val(); var h = $('#h').val(); var image = $('#preview').attr('src'); $.ajax({ type: 'POST', url: '/Image/CropAndUpload', data: { 'x': x, 'y': y, 'w': w, 'h': h, 'image': image }, success: function (data) { $('#preview').attr('src', data); }, error: function () { alert('剪切上传失败!'); } }); }); }); ``` 控制器代码: ```csharp public class ImageController : Controller { // GET: Image public ActionResult Index() { return View(); } [HttpPost] public ActionResult CropAndUpload(string x, string y, string w, string h, string image) { // 将 base64 编码转为 byte[] 数组 byte[] bytes = Convert.FromBase64String(image.Split(',')[1]); // 将 byte[] 数组转为图片对象 System.Drawing.Image img = null; using (MemoryStream ms = new MemoryStream(bytes)) { img = System.Drawing.Image.FromStream(ms); } // 对图片进行剪切 int ix = Convert.ToInt32(x); int iy = Convert.ToInt32(y); int iw = Convert.ToInt32(w); int ih = Convert.ToInt32(h); Bitmap bmp = new Bitmap(iw, ih); Graphics g = Graphics.FromImage(bmp); g.DrawImage(img, new Rectangle(0, 0, iw, ih), new Rectangle(ix, iy, iw, ih), GraphicsUnit.Pixel); // 保存剪切后的图片 string fileName = Guid.NewGuid().ToString() + ".jpg"; string filePath = Server.MapPath("~/Uploads/" + fileName); bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg); // 返回剪切后的图片路径 return Json("/Uploads/" + fileName); } } ``` 上述代码中,使用了 jCrop 插件实现图片剪切功能,使用了 System.Drawing 命名空间中的类对图片进行剪切处理,并将剪切后的图片保存在服务器端的 Uploads 文件夹中。最后,将剪切后的图片路径以 JSON 格式返回给前端页面,并在前端页面中显示剪切后的图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值