java实现图片上传后裁剪,把白色背景变成透明图(电子印章)

应用场景:企业在白纸上盖上印章,然后软件实现透明的电子印章,在后续业务流程中使用!

A4纸-----------------------------------------处理后-------------------------------------->电子印章

java实现图片上传后裁剪,把白色背景变成透明图(电子印章)_第1张图片--------> java实现图片上传后裁剪,把白色背景变成透明图(电子印章)_第2张图片

 

实现共分为3步

1. 图片上传后预览

java实现图片上传后裁剪,把白色背景变成透明图(电子印章)_第3张图片

<%@ page import="java.io.File" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="net.coobird.thumbnailator.Thumbnails" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% if(request.getMethod().toUpperCase().equals("GET")){ //GET %>


    上传图片
    
    


<% }else{ //POST file upload //commons-fileupload-1.3.2.jar //commons-io-2.5.jar //commons-logging //Thumbnails String uploadsdir = request.getSession().getServletContext().getRealPath("/uploads") + File.separator; String yyyyMM = (new SimpleDateFormat("yyyy-MM")).format(new Date()); File file = new File(uploadsdir + yyyyMM); if (!file.exists()) { file.mkdirs(); } if (file.canWrite()) { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletContext servletContext = request.getSession().getServletContext(); File repository = (File)servletContext.getAttribute("javax.servlet.context.tempdir"); factory.setRepository(repository); ServletFileUpload upload = new ServletFileUpload(factory); upload.setFileSizeMax(1024*1024*10);//最大10M try { List items = upload.parseRequest(request); Iterator iter = items.iterator(); if(iter.hasNext()) { FileItem item = (FileItem)iter.next(); String fileName; String contentType; if (!item.isFormField()) { fileName = item.getName(); contentType = item.getContentType(); if (contentType != null && "image/jpeg,image/gif,image/png,image/bmp,image/x-png,image/pjpeg".indexOf(contentType) == -1) { response.sendRedirect("index.jsp?error=1");//不是图片类型 } else { String last = fileName.substring(fileName.lastIndexOf("."), fileName.length()); String filename = UUID.randomUUID().toString().toLowerCase().replaceAll("-","") + last; File savedFile = new File(file, filename); item.write(savedFile); //压缩 width 1000 Thumbnails.of(savedFile.getAbsolutePath()).width(1000) .toFile(savedFile.getAbsoluteFile()); response.sendRedirect("corp.jsp?u=uploads/" + yyyyMM + "/" + filename); } } } } catch (Exception e) { e.printStackTrace(); response.sendRedirect("index.jsp?error=5");//上传异常 } } else { response.sendRedirect("index.jsp?error=2");//文件夹不可写 } } %>

2.预览后进行裁剪

java实现图片上传后裁剪,把白色背景变成透明图(电子印章)_第4张图片

<%@ page import="java.io.File" %>
<%@ page import="net.coobird.thumbnailator.Thumbnails" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="javax.imageio.ImageIO" %>
<%@ page import="java.awt.*" %>
<%@ page import="javax.swing.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% if(request.getMethod().toUpperCase().equals("GET")){ //GET %>


    图片裁剪
    
    
    
    
    
    



<%=request.getParameter("u") %>" id="target" alt="上传图"/>

" />
<% }else{ //POST file corp String u = request.getParameter("u"); String x1 = request.getParameter("x1"); String x2 = request.getParameter("x2"); String y1 = request.getParameter("y1"); String y2 = request.getParameter("y2"); String w = request.getParameter("w"); String h = request.getParameter("h"); if(u!=null&&w!=null&&h!=null&&x1!=null&&x2!=null) { int int_x1 = Integer.parseInt(x1); int int_y1 = Integer.parseInt(y1); int int_w = Integer.parseInt(w); int int_h = Integer.parseInt(h); String uploadsdir = request.getSession().getServletContext().getRealPath("/"); String filepath = uploadsdir + u; //crop to png File inputfile = new File(filepath); File outfile = new File(filepath.substring(0,filepath.lastIndexOf("."))+".png"); Thumbnails.of(inputfile).sourceRegion(int_x1, int_y1, int_w, int_h) .size(int_w, int_h).outputFormat("png") .toFile(outfile); //to apla transApla(outfile); //png file u=u.substring(0,u.lastIndexOf("."))+".png"; response.sendRedirect("show.jsp?u=" + u); } } %> <%! //去掉图片背景 public void transApla(File file){ InputStream is=null; try { is = new FileInputStream(file); // 如果是MultipartFile类型,那么自身也有转换成流的方法:is = file.getInputStream(); BufferedImage bi = ImageIO.read(is); Image image = (Image) bi; ImageIcon imageIcon = new ImageIcon(image); BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics(); g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver()); int alpha = 0; for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) { for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) { int rgb = bufferedImage.getRGB(j2, j1); int R = (rgb & 0xff0000) >> 16; int G = (rgb & 0xff00) >> 8; int B = (rgb & 0xff); if (((255 - R) < 30) && ((255 - G) < 30) && ((255 - B) < 30)) { rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff); } bufferedImage.setRGB(j2, j1, rgb); } } g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver()); ImageIO.write(bufferedImage, "png", file);// 直接输出文件 } catch (Exception e) { e.printStackTrace(); } finally { if(is!=null){ try{ is.close(); }catch (Exception e){} } } } %>

3.裁剪图片进行png转换,然后白色背景变透明图

java实现图片上传后裁剪,把白色背景变成透明图(电子印章)_第5张图片

<%@ page contentType="text/html;charset=UTF-8" language="java" %>



    
    预览


    <%=request.getParameter("u") %>" width="250" />
    

百度网盘:https://pan.baidu.com/s/1u-QzDZQWvnsODPfm7eaE9g

 

 

你可能感兴趣的:(java)