激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - struts2+jsp+jquery+Jcrop實(shí)現(xiàn)圖片裁剪并上傳實(shí)例

struts2+jsp+jquery+Jcrop實(shí)現(xiàn)圖片裁剪并上傳實(shí)例

2020-07-27 14:19hanamingming Java教程

本篇文章主要介紹了struts2+jsp+jquery+Jcrop實(shí)現(xiàn)圖片裁剪并上傳實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。

今天有業(yè)務(wù)需要制作用戶頭像的需求,在網(wǎng)上找了個(gè)可以裁剪大圖制作自己希望大小的圖片的方法(基于Struts2)。特此記錄一下。

不廢話,具體的步驟如下:

<1> 使用html標(biāo)簽上傳需要裁剪的大圖。

<2> 在頁(yè)面呈現(xiàn)大圖,使用Jcrop(Jquery)對(duì)大圖進(jìn)行裁剪,并且可以進(jìn)行預(yù)覽。

<3> 選擇好截取部分之后發(fā)送數(shù)據(jù)給Action,在服務(wù)器端使用 Java API 對(duì)大圖進(jìn)行裁剪。

<4> 保存大圖裁剪好的頭像到指定目錄,完成業(yè)務(wù)。

下面一步一步做:

第一步:使用html標(biāo)簽上傳需要裁剪的大圖。

這一步說(shuō)白了也就是使用Struts2自帶的FileUpload功能,把圖片進(jìn)行上傳具體代碼如下:

html頁(yè)面:

?
1
2
3
4
<form id="ulform" action="uploadPic.action" enctype="multipart/form-data" method="post">
 <input type="file" name="pic" id="file" value="選擇圖片" />
 <input type="submit" value="點(diǎn)擊上傳" />
</form>

Struts2配置文件

?
1
2
3
4
<action name="uploadPic" class="com.luoxiao.tbms.user.action.UserAction" method="uploadPic">
 <result name="success" type="redirect">changePic.jsp</result>
 <result name="error">changePic.jsp</result>
</action>

根據(jù)配置點(diǎn)擊提交按鈕,會(huì)提交表單,把圖片以流的形式發(fā)送給 UserAction的uploadPic方法,該方法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class UserAction{
 private File pic; //(在此省略 get 和 set 方法)
 private String picFileName; //(省略get和set方法, 該屬性Struts2會(huì)自動(dòng)賦值為上傳文件的文件名)
 public String uploadPic() {
  String[] str = { ".jpg", ".jpeg", ".bmp", ".gif" };
  // 獲取用戶登錄名
  TbUser curruser = (TbUser) getValue(SCOPE_SESSION, "curruser");
  // 限定文件大小是4MB
  if (pic == null || pic.length() > 4194304) {
   //文件過(guò)大
   return "error";
  }
  for (String s : str) {
   if (picFileName.endsWith(s)) {
    String realPath = ServletActionContext.getServletContext().getRealPath("/uploadpic");// 在tomcat中保存圖片的實(shí)際路徑 == "webRoot/uploadpic/"
    File saveFile = new File(new File(realPath), "新文件名.jpg"); // 在該實(shí)際路徑下實(shí)例化一個(gè)文件
    // 判斷父目錄是否存在
    if (!saveFile.getParentFile().exists()) {
     saveFile.getParentFile().mkdirs();
    }
    try {
     // 執(zhí)行文件上傳
     // FileUtils 類名 org.apache.commons.io.FileUtils;
     // 是commons-io包中的,commons-fileupload 必須依賴
     // commons-io包實(shí)現(xiàn)文件上次,實(shí)際上就是將一個(gè)文件轉(zhuǎn)換成流文件進(jìn)行讀寫(xiě)
     FileUtils.copyFile(pic, saveFile);
    } catch (IOException e) {
     return "imageError";
    }
   }
  }
  return "success";
 }
}

這樣就可以把用戶選擇的圖片上傳到tomcat的webRoot/uploadpic/文件夾下。 然后訪問(wèn)頁(yè)面,頁(yè)面中就可以顯示出剛剛上傳的大圖了。代碼如下。

  1. <div style="width: 500px; height: 500px;"
  2.  <img style="margin-top:20px;" src="../uploadpic/上傳文件名稱.jpg"/>" id="target" alt="" />  
  3. </div> 
 

第一步完成。

第二步:使用Jcrop插件裁剪該圖片,并且在頁(yè)面中預(yù)覽。

Jcrop是一個(gè)基于JQuery的成熟的圖片裁剪的插件。如圖:

struts2+jsp+jquery+Jcrop實(shí)現(xiàn)圖片裁剪并上傳實(shí)例

該插件使用比較簡(jiǎn)單:

<1> 在裁剪圖片頁(yè)面中,引入兩個(gè)js文件,和1個(gè)Jcrop需要的css文件(Jcrop包中有,注意引入順序,先引入jquery):

  1. <script src="../js/jquery-1.8.3.min.js" type="text/javascript"></script>  
  2. <script src="../js/jquery.Jcrop.js" type="text/javascript"></script> 
  3. <link rel="stylesheet" href="../css/jquery.Jcrop.css" type="text/css" /> 
 

<2> 在html頁(yè)面中按照J(rèn)crop要求的格式編寫(xiě)兩個(gè)img標(biāo)簽,一個(gè)用作裁剪后的預(yù)覽,一個(gè)用作顯示大圖,代碼如下:

  1. 預(yù)覽: 
  2. <div style="width:200px;height:200px;overflow:hidden; border:1px solid gray;">  
  3.  <img id="preview" width="200px" height="200px" /> 
  4. </div>  
  5. 原圖: 
  6.  <img src="../uploadpic/上傳大圖.jpg" id="target" alt="" /> 
 

<3> 在該頁(yè)面中寫(xiě)js代碼,使其可以裁剪圖片并且預(yù)覽:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<script type="text/javascript">
  var x;
 var y;
 var width;
 var height;
 $(function(){
  var jcrop_api, boundx, boundy;
  //使原圖具有裁剪功能
  $('#target').Jcrop({
   onChange: updatePreview,
   onSelect: updatePreview,
   aspectRatio: 1
  },function(){
   // Use the API to get the real image size
   var bounds = this.getBounds();
   boundx = bounds[0];
   boundy = bounds[1];
   // Store the API in the jcrop_api variable
   jcrop_api = this;
  });
  //裁剪過(guò)程中,每改變裁剪大小執(zhí)行該函數(shù)
  function updatePreview(c){
   if (parseInt(c.w) > 0){
    $('#preview').css({
     width: Math.round(<span style="color:#ff0000;">200 </span>/ c.w * boundx) + 'px', <span style="color:#ff0000;">//200 為預(yù)覽div的寬和高</span>
     height: Math.round(<span style="color:#ff0000;">200 </span>/ c.h * boundy) + 'px',
     marginLeft: '-' + Math.round(200 / c.w * c.x) + 'px',
     marginTop: '-' + Math.round(200 / c.h * c.y) + 'px'
    });
    $('#width').val(c.w); //c.w 裁剪區(qū)域的寬
    $('#height').val(c.h); //c.h 裁剪區(qū)域的高
    $('#x').val(c.x); //c.x 裁剪區(qū)域左上角頂點(diǎn)相對(duì)于圖片左上角頂點(diǎn)的x坐標(biāo)
    $('#y').val(c.y); //c.y 裁剪區(qū)域頂點(diǎn)的y坐標(biāo)
   }
   };
 });
 </script>

至此我們已經(jīng)可以看到裁剪之后的樣子了,并且也可以得到裁剪區(qū)域的x,y,height,width屬性。

第三步:把截取的該區(qū)域的屬性傳遞給action,讓action根據(jù)所得屬性,利用javaAPI把原圖裁剪成小圖。

<1> 設(shè)置form表單與隱藏域表單組件,并且在裁剪的時(shí)候?qū)υ撍膫€(gè)組件的value屬性賦值

?
1
2
3
4
5
6
7
8
9
<form action="cutPic.action" method="post">
 點(diǎn)擊
 <input type="hidden" name="image.x" id="x"/>
 <input type="hidden" name="image.y" id="y"/>
 <input type="hidden" name="image.width" id="width"/>
 <input type="hidden" name="image.height" id="height"/>
 <input type="submit" value="確定" />
 ,設(shè)置完成。
</form>

<2> 點(diǎn)擊確定,提交該表單,訪問(wèn)action,配置如下:

?
1
2
3
<action name="cutPic" class="com.luoxiao.tbms.user.action.UserAction" method="cutPic">
 <result name="success" type="redirectAction">../announcement/announcement_list.action</result>
</action>

<3>Struts2帶著四個(gè)參數(shù)訪問(wèn)UserAction,并且會(huì)自動(dòng)給UserAction中的image屬性賦值,該image屬性為OperateImage的一個(gè)實(shí)例對(duì)象,該類為裁剪圖片類,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.luoxiao.util;
 
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 OperateImage {
 // ===源圖片路徑名稱如:c:.jpg
 private String srcpath;
 // ===剪切圖片存放路徑名稱.如:c:.jpg
 private String subpath;
 // ===剪切點(diǎn)x坐標(biāo)
 private int x;
 private int y;
 // ===剪切點(diǎn)寬度
 private int width;
 private int height;
 public OperateImage() {
 }
 /** 對(duì)圖片裁剪,并把裁剪完的新圖片保存 */
 public void cut() throws IOException {
  FileInputStream is = null;
  ImageInputStream iis = null;
  try {
   // 讀取圖片文件
   is = new FileInputStream(srcpath);
   /*
    * 返回包含所有當(dāng)前已注冊(cè) ImageReader 的 Iterator,這些 ImageReader 聲稱能夠解碼指定格式。
    * 參數(shù):formatName - 包含非正式格式名稱 . (例如 "jpeg" 或 "tiff")等 。
    */
   Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg");
   ImageReader reader = it.next();
   // 獲取圖片流
   iis = ImageIO.createImageInputStream(is);
   /*
    * <p>iis:讀取源.true:只向前搜索 </p>.將它標(biāo)記為 ‘只向前搜索'。
    * 此設(shè)置意味著包含在輸入源中的圖像將只按順序讀取,可能允許 reader 避免緩存包含與以前已經(jīng)讀取的圖像關(guān)聯(lián)的數(shù)據(jù)的那些輸入部分。
    */
   reader.setInput(iis, true);
   /*
    * <p>描述如何對(duì)流進(jìn)行解碼的類<p>.用于指定如何在輸入時(shí)從 Java Image I/O
    * 框架的上下文中的流轉(zhuǎn)換一幅圖像或一組圖像。用于特定圖像格式的插件 將從其 ImageReader 實(shí)現(xiàn)的
    * getDefaultReadParam 方法中返回 ImageReadParam 的實(shí)例。
    */
   ImageReadParam param = reader.getDefaultReadParam();
   /*
    * 圖片裁剪區(qū)域。Rectangle 指定了坐標(biāo)空間中的一個(gè)區(qū)域,通過(guò) Rectangle 對(duì)象
    * 的左上頂點(diǎn)的坐標(biāo)(x,y)、寬度和高度可以定義這個(gè)區(qū)域。
    */
   Rectangle rect = new Rectangle(x, y, width, height);
   // 提供一個(gè) BufferedImage,將其用作解碼像素?cái)?shù)據(jù)的目標(biāo)。
   param.setSourceRegion(rect);
   /*
    * 使用所提供的 ImageReadParam 讀取通過(guò)索引 imageIndex 指定的對(duì)象,并將 它作為一個(gè)完整的
    * BufferedImage 返回。
    */
   BufferedImage bi = reader.read(0, param);
   // 保存新圖片
   ImageIO.write(bi, "jpg", new File(subpath));
  } finally {
   if (is != null)
    is.close();
   if (iis != null)
    iis.close();
  }
 
 }
}

<4> 給該類的實(shí)例的四個(gè)屬性 x,y,width,height賦值之后,訪問(wèn)action中的cutPic方法,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class UserAction extends BaseAction {
 private OperateImage image;(省略get set)
 
 private File pic; // 接收這個(gè)上傳的文件
 private String picFileName; // Struts2提供的格式,在文件名后+FileName就是上傳文件的名字
 
 /**
  * 裁剪頭像
  */
 public String cutPic(){
        String name = ServletActionContext.getServletContext().getRealPath("/uploadpic/原圖名.jpg");
  image.setSrcpath(name);
  image.setSubpath(ServletActionContext.getServletContext().getRealPath("/uploadpic/裁剪目標(biāo)圖名.jpg"));
  try {
   image.cut(); //執(zhí)行裁剪操作 執(zhí)行完后即可生成目標(biāo)圖在對(duì)應(yīng)文件夾內(nèi)。
  } catch (IOException e) {
   e.printStackTrace();
  }
  return "success";
 }
}

第四步:把截取好的頭像保存在具體文件夾下即可,裁剪過(guò)程完成。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/csd_xuming/article/details/8848939

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 爱高潮www亚洲精品 国产一区二区三区视频免费 | 性少妇freeseⅹbbwhd | 97zyz成人免费视频 | 九九热精品在线 | 国产亚洲精品久久久久5区 男人天堂免费 | 黄色免费播放网站 | 精品亚洲夜色av98在线观看 | 日本免费aaa观看 | 在线中文字幕观看 | 黄色网址免费入口 | 免费视频观看 | 综合精品一区 | 草久免费| 日韩av片网站 | 国产精品视频中文字幕 | 日本精品婷婷久久爽一下 | 法国性xxx精品hd专区 | 最新中文字幕免费视频 | 久久久久久久久国产 | 精国品产一区二区三区有限公司 | 他也色在线视频 | 九一成人 | 天天操天天看 | 色人阁五月天 | 欧美日韩在线免费观看 | 免费99热在线观看 | 国产一级aaa全黄毛片 | 亚洲成人午夜精品 | a级黄色片视频 | 久久久精品视频免费看 | 爱性久久久久久久 | 日产精品久久久一区二区福利 | 精品成人av一区二区在线播放 | 久久久婷婷 | 国语自产免费精品视频在 | 激情影院在线观看 | 免费看污视频在线观看 | 亚洲一区二区三区高清视频 | 色婷婷综合久久久中字幕精品久久 | 网站久久 | 亚洲电影免费观看国语版 |