1.下载最新的Jcrop文件。
http://deepliquid.com/content/Jcrop.html
- public class Utils {
- public static String getExtension(File f) {
- return (f != null) ? getExtension(f.getName()) : "";
- }
- public static String getExtension(String filename) {
- return getExtension(filename, "");
- }
- public static String getExtension(String filename, String defExt) {
- if ((filename != null) && (filename.length() > 0)) {
- int i = filename.lastIndexOf('.');
- if ((i >-1) && (i < (filename.length() - 1))) {
- return filename.substring(i + 1);
- }
- }
- return defExt;
- }
- public static String trimExtension(String filename) {
- if ((filename != null) && (filename.length() > 0)) {
- int i = filename.lastIndexOf('.');
- if ((i >-1) && (i < (filename.length()))) {
- return filename.substring(0, i);
- }
- }
- return filename;
- }
- }
- public class SaveImage{
- /**
- * 保存图片
- * @param img 原图路径
- * @param dest 目标图路径
- * @param top 选择框的左边y坐标
- * @param left 选择框的左边x坐标
- * @param width 选择框宽度
- * @param height 选择框高度
- * @return
- * @throws IOException
- */
- public static boolean saveImage(File img,
- String dest,
- int top,
- int left,
- int width,
- int height) throws IOException {
- File fileDest = new File(dest);
- if(!fileDest.getParentFile().exists())
- fileDest.getParentFile().mkdirs();
- String ext = Utils.getExtension(dest).toLowerCase();
- BufferedImage bi = (BufferedImage)ImageIO.read(img);
- height = Math.min(height, bi.getHeight());
- width = Math.min(width, bi.getWidth());
- if(height <= 0) height = bi.getHeight();
- if(width <= 0) width = bi.getWidth();
- top = Math.min(Math.max(0, top), bi.getHeight()-height);
- left = Math.min(Math.max(0, left), bi.getWidth()-width);
- BufferedImage bi_cropper = bi.getSubp_w_picpath(left, top, width, height);
- return ImageIO.write(bi_cropper, ext.equals("png")?"png":"jpeg", fileDest);
- }
- public static void main(String[] args) {
- try {
- System.out.println(saveImage(new File("E:\\JavaWork\\pic\\WebRoot\\css\\flowers.jpg"),"E:\\JavaWork\\pic\\WebRoot\\css\\flowers1.jpg",106,87,289,217));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
这里的top、left、width、height都可以直接用Jcrop里获取到。 在Jcrop的“Basic Handler”这个demo里面,相应的X1、Y1、W、H这四个参数,用request可以得到这些值。