Java如何實(shí)現(xiàn)圖片裁剪預(yù)覽功能
在項(xiàng)目中,我們需要做些類(lèi)似頭像上傳,圖片裁剪的功能,ok看下面文章!
需要插件:jQuery Jcrop
后端代碼:
package org.csg.upload;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class Upload {
/**
* @author 小夜的傳說(shuō)
* @param path1 圖片原路徑
* @param path2 裁剪后存儲(chǔ)的路徑
* @param x x軸
* @param y y軸
* @param w
* @param h
*/
public static void CutImage(String path1,String path2,int x,int y,int w,int h){
FileInputStream fileInputStream=null;
ImageInputStream iis=null;
try {
//讀取圖片文件,建立文件輸入流
fileInputStream=new FileInputStream(path1);
//創(chuàng)建圖片的文件流 迭代器
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader=it.next();
//獲取圖片流 建立文圖 文件流
iis=ImageIO.createImageInputStream(fileInputStream);
//獲取圖片默認(rèn)參數(shù)
reader.setInput(iis, true);
ImageReadParam param=reader.getDefaultReadParam();
//定義裁剪區(qū)域
Rectangle rect=new Rectangle(x,y,w,h);
param.setSourceRegion(rect);
BufferedImage bi=reader.read(0,param);
ImageIO.write(bi, "jpg", new File(path2));
} catch (Exception e) {
e.printStackTrace();
System.out.println("裁剪失敗");
}finally{
try {
if(fileInputStream!=null){
fileInputStream.close();
}
if(iis!=null){
iis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
訪問(wèn)代碼:
<%@ page language="java" import="java.util.*,org.csg.upload.*" pageEncoding="utf-8"%>
<%
//圖片的相對(duì)路徑
String imagPath=request.getParameter("imgPath");
String relPath=request.getRealPath("/");//獲取圖片服務(wù)器絕對(duì)地址
String newFileName=new Date().getTime()+".jpg";
//實(shí)際圖片路徑
String path1=relPath+imagPath;
//裁剪后存儲(chǔ)到服務(wù)器的圖片路徑
String path2=relPath+"/images/"+newFileName;
int x=Integer.parseInt(request.getParameter("x"));
int y=Integer.parseInt(request.getParameter("y"));
int w=Integer.parseInt(request.getParameter("w"));
int h=Integer.parseInt(request.getParameter("h"));
try{
Upload.CutImage(path1, path2, x, y, w, h);
out.print("<img src='images/"+newFileName+"'/>");
}catch(Exception e){
e.printStackTrace();
out.print("圖片裁剪失敗");
}
%>
jsp代碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Jsp開(kāi)發(fā)頭像裁剪</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.Jcrop.min.js"></script>
<style type="text/css">
*{margin: 0;padding: 0;}
.cut{
margin-top: 20px;
}
#preview-pane {
display: block;
position: absolute;
z-index: 2000;
top: 10px;
right: -280px;
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);
}
#preview-pane .preview-container {
width: 250px;
height: 170px;
overflow: hidden;
}
</style>
<script type="text/javascript">
$(function(){
var jcrop_api,
boundx="",
boundy="",
$preview = $('#preview-pane'),
$pcnt = $('#preview-pane .preview-container'),
$pimg = $('#preview-pane .preview-container img'),
xsize = $pcnt.width(),
ysize = $pcnt.height();
$('#cutImage').Jcrop({
onChange:showCoords,//獲取選中的值
onSelect:showCoords,//獲取拖拽的值
aspectRatio: xsize / ysize
},function(){
var bounds = this.getBounds();
boundx = bounds[0];
boundy = bounds[1];
jcrop_api = this;
$preview.appendTo(jcrop_api.ui.holder);
});
function showCoords(c){
var x=c.x;
var y=c.y;
var w=c.w;
var h=c.h;
$("#x1").val(parseInt(x));
$("#y1").val(parseInt(y));
$("#w").val(parseInt(w));
$("#h").val(parseInt(h));
if (parseInt(c.w) > 0){
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'
});
}
}
});
</script>
</head>
<body>
<h1>Java開(kāi)發(fā)QQ頭像裁剪系統(tǒng)</h1>
<div class="cut">
<img id="cutImage" alt="" src="images/1.jpg" >
<div id="preview-pane">
<div class="preview-container">
<img src="images/1.jpg" class="jcrop-preview" alt="Preview" />
</div>
</div>
</div>
<form action="success.jsp" method="post" >
<input type="text" value="images/1.jpg" name="imgPath">
x軸:<input type="text" size="4" id="x1" name="x" />
y軸:<input type="text" size="4" id="y1" name="y"/>
寬度:<input type="text" size="4" id="w" name="w"/>
高度:<input type="text" size="4" id="h" name="h"/>
<input type="submit" value="裁剪"/>
</form>
</body>
</html>
效果圖:

以上就是本文的全部?jī)?nèi)容,希望大家能夠喜歡。
相關(guān)文章
Spring?MVC異步上傳、跨服務(wù)器上傳和文件下載功能實(shí)現(xiàn)
這篇文章主要介紹了Spring?MVC異步上傳、跨服務(wù)器上傳和文件下載功能實(shí)現(xiàn),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示功能(詳細(xì)步驟)
這篇文章主要介紹了SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示,通過(guò)數(shù)據(jù)庫(kù)設(shè)計(jì)、實(shí)現(xiàn)數(shù)據(jù)訪問(wèn)層、業(yè)務(wù)邏輯層和控制層的代碼編寫(xiě),以及前端頁(yè)面的開(kāi)發(fā),本文詳細(xì)地介紹了SpringBoot整合Echarts的實(shí)現(xiàn)步驟和代碼,需要的朋友可以參考下2023-05-05
Mybatis中如何設(shè)置sqlSession自動(dòng)提交
在MyBatis中,默認(rèn)情況下,獲取的SqlSession對(duì)象不會(huì)自動(dòng)提交事務(wù),這意味著在進(jìn)行更新、刪除或插入等操作后,需要顯式調(diào)用commit方法來(lái)提交事務(wù),但是,可以在獲取SqlSession時(shí)通過(guò)將openSession方法的參數(shù)設(shè)置為true2024-09-09
使用JPA自定義VO接收返回結(jié)果集(unwrap)
這篇文章主要介紹了使用JPA自定義VO接收返回結(jié)果集(unwrap),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
java8實(shí)現(xiàn)list集合中按照某一個(gè)值相加求和,平均值等操作代碼
這篇文章主要介紹了java8實(shí)現(xiàn)list集合中按照某一個(gè)值相加求和,平均值等操作代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
springboot中不能獲取post請(qǐng)求參數(shù)的解決方法
這篇文章主要介紹了springboot中不能獲取post請(qǐng)求參數(shù)的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
java中hashCode方法與equals方法的用法總結(jié)
總的來(lái)說(shuō),Java中的集合(Collection)有兩類(lèi),一類(lèi)是List,再有一類(lèi)是Set。前者集合內(nèi)的元素是有序的,元素可以重復(fù);后者元素?zé)o序,但元素不可重復(fù)2013-10-10
Java設(shè)計(jì)模式之創(chuàng)建者模式簡(jiǎn)介
這篇文章主要介紹了Java設(shè)計(jì)模式之創(chuàng)建者模式,需要的朋友可以參考下2014-07-07

