Image Cut

Upload pictures class

package com.cmcc.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;

/*****************************************************
 * 
 * @author wuzhenzhong
 * 
 * @since 2009-3-10
 * 
 *****************************************************/
public class UpLoadUserHeadImage extends HttpServlet {

        
        
        /**
         *  Upload the picture class  
         */
        private static final long serialVersionUID = -3421050378459224321L;
        public UpLoadUserHeadImage() {
                config = null;
                FileName = null;
                sPath = "/UploadPhoto";
        }

        public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
                SmartUpload mySmartUpload = new SmartUpload();
                mySmartUpload.initialize(config, request, response);
                mySmartUpload.setMaxFileSize(0x200000L);
                mySmartUpload.setAllowedFilesList("jpg,gif,png,jpeg,bmp");
                try {
                        mySmartUpload.upload();
                        File myFile = mySmartUpload.getFiles().getFile(0);
                        if (!myFile.isMissing()) {
                                Date currTime = new Date();
                                SimpleDateFormat formatter2 = new SimpleDateFormat(
                                                "yyyyMMddhhmmssS", Locale.US);
                                FileName = new String(formatter2.format(currTime).getBytes(
                                                "iso-8859-1"));
                                String ext = myFile.getFileExt();
                                FileName = (new StringBuilder(String.valueOf(FileName)))
                                                .append(".").append(ext).toString();
                                myFile.saveAs((new StringBuilder(String.valueOf(sPath)))
                                                .append("/").append(FileName).toString(), 1);
                        }
                        response.sendRedirect((new StringBuilder(
                                        "/upload/uploadimage.jsp?Picurl=")).append(FileName)
                                        .append("&step=2").toString());
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public void init(ServletConfig config) throws ServletException {
                this.config = config;
        }

        private ServletConfig config;
        private String FileName;
        private String sPath;
}


Image processing class is used to cut pictures

package com.cmcc.servlet;

import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cmcc.util.ImageHepler;

/*****************************************************
 * 
 * @author wuzhenzhong
 * 
 * @since 2009-3-10
 * 
 *****************************************************/
public class ZoomImage extends HttpServlet {

        /**
         * 
         */
        private static final long serialVersionUID = 3424060966540179365L;

        public ZoomImage() {
        }

        public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
                int imageWidth = Integer.parseInt(request.getParameter("txt_width"));
                int imageHeight = Integer.parseInt(request.getParameter("txt_height"));
                int cutTop = Integer.parseInt(request.getParameter("txt_top"));
                int cutLeft = Integer.parseInt(request.getParameter("txt_left"));
                int dropWidth = Integer.parseInt(request.getParameter("txt_DropWidth"));
                int dropHeight = Integer.parseInt(request
                                .getParameter("txt_DropHeight"));
                float imageZoom = Float.parseFloat(request.getParameter("txt_Zoom"));
                String picture = request.getParameter("picture");
                System.out.println((new StringBuilder("imageWidth : ")).append(
                                imageWidth).toString());
                System.out.println((new StringBuilder("imageHeight : ")).append(
                                imageHeight).toString());
                System.out.println((new StringBuilder("cutTop : ")).append(cutTop)
                                .toString());
                System.out.println((new StringBuilder("cutLeft : ")).append(cutLeft)
                                .toString());
                System.out.println((new StringBuilder("dropWidth : "))
                                .append(dropWidth).toString());
                System.out.println((new StringBuilder("dropHeight : ")).append(
                                dropHeight).toString());
                System.out.println((new StringBuilder("imageZoom : "))
                                .append(imageZoom).toString());
                System.out.println((new StringBuilder("picture : ")).append(picture)
                                .toString());
                System.out.println((new StringBuilder("url :")).append(
                                request.getRealPath("")).append("/UploadPhoto/")
                                .append(picture).toString());
                Rectangle rec = new Rectangle(cutLeft, cutTop, dropWidth, dropHeight);
                File file = new File((new StringBuilder(String.valueOf(request
                                .getRealPath("")))).append("/User/UserHeadImage/").append(
                                picture).toString());
                saveSubImage(new File((new StringBuilder(String.valueOf(request
                                .getRealPath("")))).append("/UploadPhoto/").append(picture)
                                .toString()), file, imageWidth, imageHeight, rec);
                response.sendRedirect((new StringBuilder(
                                "/upload/uploadimage.jsp?Picurl=")).append(picture)
                                .append("&step=3").toString());
        }

        private static void saveSubImage(File srcImageFile, File descDir,
                        int width, int height, Rectangle rect) throws IOException {
                ImageHepler.cut(srcImageFile, descDir, width, height, rect);
        }
}

Cut and save images for the pictures

package com.cmcc.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

/*****************************************************
 * 
 * @author wuzhenzhong
 * 
 * @since 2009-3-10
 * 
 *****************************************************/
public class ImageHepler {

        public ImageHepler() {
        }

        private static BufferedImage makeThumbnail(Image img, int width, int height) {
                BufferedImage tag = new BufferedImage(width, height, 1);
                Graphics g = tag.getGraphics();
                g.drawImage(img.getScaledInstance(width, height, 4), 0, 0, null);
                g.dispose();
                return tag;
        }

        private static void saveSubImage(BufferedImage image,
                        Rectangle subImageBounds, File subImageFile) throws IOException {
                String fileName = subImageFile.getName();
                String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
                BufferedImage subImage = new BufferedImage(subImageBounds.width,
                                subImageBounds.height, 1);
                Graphics g = subImage.getGraphics();
                if (subImageBounds.width > image.getWidth()
                                || subImageBounds.height > image.getHeight()) {
                        int left = subImageBounds.x;
                        int top = subImageBounds.y;
                        if (image.getWidth() < subImageBounds.width)
                                left = (subImageBounds.width - image.getWidth()) / 2;
                        if (image.getHeight() < subImageBounds.height)
                                top = (subImageBounds.height - image.getHeight()) / 2;
                        g.setColor(Color.white);
                        g.fillRect(0, 0, subImageBounds.width, subImageBounds.height);
                        g.drawImage(image, left, top, null);
                        ImageIO.write(image, formatName, subImageFile);
                        System.out.println((new StringBuilder("if is running left:"))
                                        .append(left).append(" top: ").append(top).toString());
                } else {
                        g.drawImage(image.getSubimage(subImageBounds.x, subImageBounds.y,
                                        subImageBounds.width, subImageBounds.height), 0, 0, null);
                        System.out.println("else is running");
                }
                g.dispose();
                ImageIO.write(subImage, formatName, subImageFile);
        }

        public static void cut(String srcImageFile, String descDir, int width,
                        int height, Rectangle rect) throws IOException {
                Image image = ImageIO.read(new File(srcImageFile));
                BufferedImage bImage = makeThumbnail(image, width, height);
                saveSubImage(bImage, rect, new File(descDir));
        }

        public static void cut(File srcImageFile, File descDir, int width,
                        int height, Rectangle rect) throws IOException {
                Image image = ImageIO.read(srcImageFile);
                BufferedImage bImage = makeThumbnail(image, width, height);
                saveSubImage(bImage, rect, descDir);
        }
}
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Image Cut

  • hibernate generic generic DAO

    hibernate generic generic DAO

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and oth ...

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries