- 浏览: 232238 次
- 性别:
- 来自: 成都
文章分类
- 全部博客 (70)
- Spring (5)
- J2EE (5)
- Android (21)
- JavaSe (12)
- JmagicK (1)
- Eclipse (3)
- 重构 (1)
- Struts2 (2)
- 项目经验 (2)
- UML (1)
- 设计模式 (3)
- TDD (1)
- 极限编程 (1)
- 敏捷编程 (1)
- Json (1)
- Java web (1)
- ejb (1)
- mongodb (1)
- Mylyn (1)
- git (1)
- oracle (2)
- 操作系统 (1)
- 数据结构与算法 (2)
- C (1)
- 效率参考 (1)
- Java基础 (3)
- 开源框架 (1)
- 阿里云 (2)
- linux (1)
- html5开发框架 (3)
- ios开发 (2)
- webapp (0)
- 我的创业 (1)
- Java (1)
- IDE (1)
- Java虚拟机 (2)
- 区块链 (2)
最新评论
-
BlueSkyXin:
你真的很厉害,懂得不懂得,都跟我们讲清楚了。你这才叫学知识,透 ...
Struts2值栈的理解 -
wiseyl:
第一种方法: 过滤器不太好,建议在service层时 由于se ...
hibernate+spring mvc,解决hibernate对象懒加载,json序列化失败 -
u012256814:
您好,看了您的这个教程受益不小,我就是想问问您在sha1后面加 ...
Google Map API V2 密钥申请 详细图解 -
ahau10:
OGNL表达式,context, valueStack都讲清楚 ...
Struts2值栈的理解 -
yaolan:
Struts2值栈的理解
最近要实现把用户上传的图片转换为500宽,245*200,102*85,大小的格式,要保证图片质量,又要高效率。下面是我试过的一下方法。
方法一:效率高,质量低
方法二:质量高,效率低
在网上找了找相关的资料,发现有个开源的框架jmagick.org,http://www.jmagick.org/lenya/jmagick/live/download.html,正在验证中……
方法一:效率高,质量低
import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageUtils{ /** * 对图片进行缩小 * @param originalImage 原始图片 * @param times 缩小倍数 * @return 缩小后的图像 */ public static BufferedImage zoomImage(BufferedImage originalImage,double times){ int width = (int) ((int)originalImage.getWidth()*times); int height = (int) ((int)originalImage.getHeight()*times); System.out.println("width:"+width+" height:"+height); BufferedImage newImage = new BufferedImage(width,height,originalImage.getType()); Graphics g = newImage.getGraphics(); g.drawImage(originalImage, 0,0,width,height,null); g.dispose(); return newImage; } /** * 截取图片 * @param path 图片路径 * @param x 开始位置x坐标 * @param y 开始位置y坐标 * @param width 宽度 * @param height 高度 * @return 图片保存的url */ public static BufferedImage cutting(BufferedImage bufferedImage,int x,int y,int width,int height){ return bufferedImage.getSubimage(x, y, width, height); } /** * 处理图片 获得102*85,245*200,宽500等比例缩放 * @param path 图片路径 * @return 处理成功返回true,处理失败返回false */ public static boolean afterPostPhoto(String path){ BufferedImage bufferedImage = null; String suffix=path.substring(path.lastIndexOf(".")+1, path.length()); try { bufferedImage = ImageIO.read(new File(path)); double x=bufferedImage.getWidth(); double y=bufferedImage.getHeight(); System.out.println("x="+x+",y="+y); if(x>500){ double times=500/x; ImageIO.write(zoomImage(bufferedImage, times), suffix, new File(path.substring(0, path.lastIndexOf("."))+"L"+path.substring(path.lastIndexOf("."), path.length()))); } if(x>245){ double times; if(x/y>1.225){ times=200/y; }else{ times=245/x; } ImageIO.write(cutting(zoomImage(bufferedImage, times), 0, 0, 245, 200), suffix, new File(path.substring(0, path.lastIndexOf("."))+"M"+path.substring(path.lastIndexOf("."), path.length()))); } if(x>102){ double times; if(x/y>1.2){ times=85/y; }else{ times=102/x; } ImageIO.write(cutting(zoomImage(bufferedImage, times), 0, 0, 102, 85), suffix, new File(path.substring(0, path.lastIndexOf("."))+"S"+path.substring(path.lastIndexOf("."), path.length()))); } } catch (IOException e) { e.printStackTrace(); return false; } return true; } public static boolean afterSetIcon(String path){ BufferedImage bufferedImage = null; double times; String suffix=path.substring(path.lastIndexOf(".")+1, path.length()); try { bufferedImage = ImageIO.read(new File(path)); } catch (IOException e) { e.printStackTrace(); return false; } double x=bufferedImage.getWidth(); double y=bufferedImage.getHeight(); if(x/y>1){ times=x/y; } else if(y/x>1){ times=y/x; } return true; } /** * 检查图片分辨率是否大于300*200 * 大于300*200返回true,执行上传,否则返回 * false并删除图片 * @param path 图片路径 * @return 大于300*200返回true,否则返回false */ public static boolean checkUpload(String path){ BufferedImage bufferedImage = null; try { bufferedImage = ImageIO.read(new File(path)); double x=bufferedImage.getWidth(); double y=bufferedImage.getHeight(); if(x<300||y<200){ return false; } } catch (IOException e) { e.printStackTrace(); return false; } return true; } public static void main(String[] args) { afterPostPhoto("F:/照片/春天的足迹/1.jpg"); // System.out.println(x/y); }
方法二:质量高,效率低
package image.too; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageOutputStream; import javax.swing.*; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.Kernel; import java.awt.image.ConvolveOp; public class ImagePro { /** * 图片缩放 * * @param originalFile * 源文件 * @param resizedFile * 目标文件 * @param newWidth * 新图片的宽度 * @param quality * 成像质量 * @throws IOException */ public static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException { BufferedImage bufferedImage = resize(originalFile, newWidth, quality); savaImage(bufferedImage, resizedFile); } public static void savaImage(BufferedImage bufferedImage, File resizedFile) throws ImageFormatException, IOException { FileOutputStream out = new FileOutputStream(resizedFile); // Encodes image as a JPEG data stream JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder .getDefaultJPEGEncodeParam(bufferedImage); param.setQuality(1.0f, true); encoder.setJPEGEncodeParam(param); encoder.encode(bufferedImage); } /** 截取图片 */ public static void cutting(File file, File newFile, int x, int y, int width, int height) { try { String endName = file.getName(); endName = endName.substring(endName.lastIndexOf(".") + 1); Iterator<ImageReader> readers = ImageIO .getImageReadersByFormatName(endName); ImageReader reader = (ImageReader) readers.next(); InputStream is = new FileInputStream(file); ImageInputStream iis = ImageIO.createImageInputStream(is); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); Rectangle rect = new Rectangle(x, y, width, height); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageOutputStream out = ImageIO .createImageOutputStream(new FileOutputStream(newFile)); ImageIO.write(bi, endName, out); } catch (Exception e) { e.printStackTrace(); } } public static BufferedImage resize(File originalFile, int newWidth, float quality) throws IOException { if (quality > 1) { throw new IllegalArgumentException( "Quality has to be between 0 and 1"); } ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath()); Image i = ii.getImage(); Image resizedImage = null; int iWidth = i.getWidth(null); int iHeight = i.getHeight(null); if (iWidth > iHeight) { resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_AREA_AVERAGING); } else { resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_AREA_AVERAGING); } // This code ensures that all the pixels in the image are loaded. Image temp = new ImageIcon(resizedImage).getImage(); // Create the buffered image. BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB); // Copy image to buffered image. Graphics g = bufferedImage.createGraphics(); // Clear background and paint the image. g.setColor(Color.white); g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); g.drawImage(temp, 0, 0, null); g.dispose(); // Soften. float softenFactor = 0.05f; float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 }; Kernel kernel = new Kernel(3, 3, softenArray); ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); bufferedImage = cOp.filter(bufferedImage, null); return bufferedImage; } /** * 处理图片 获得102*85,245*200,宽500等比例缩放 * * @param path * 图片路径 * @return 处理成功返回true,处理失败返回false */ public static boolean process(String path) { File file = new File(path); BufferedImage bufferedImage = null; try { bufferedImage = ImageIO.read(new File(path)); double x = bufferedImage.getWidth(); double y = bufferedImage.getHeight(); System.out.println("x=" + x + ",y=" + y); if (x > 500) { resize(file, new File(path.substring(0, path.lastIndexOf(".")) + "L" + path.substring(path.lastIndexOf("."), path.length())), 500, 1.0f); } else { copyImage( file, new File(path.substring(0, path.lastIndexOf(".")) + "L" + path.substring(path.lastIndexOf("."), path.length()))); } if (x > 245) { int iWidth; if (x / y > 1.225) { iWidth = (int) (x * 200 / y); } else { iWidth = 245; } resize(file, new File(path.substring(0, path.lastIndexOf(".")) + "M" + path.substring(path.lastIndexOf("."), path.length())), iWidth, 1.0f); } else { copyImage( file, new File(path.substring(0, path.lastIndexOf(".")) + "M" + path.substring(path.lastIndexOf("."), path.length()))); } if (x > 102) { int iWidth; if (x / y > 1.2) { iWidth = (int) (x * 85 / y); } else { iWidth = 102; } resize(file, new File(path.substring(0, path.lastIndexOf(".")) + "S" + path.substring(path.lastIndexOf("."), path.length())), iWidth, 1.0f); } else { copyImage( file, new File(path.substring(0, path.lastIndexOf(".")) + "S" + path.substring(path.lastIndexOf("."), path.length()))); } } catch (IOException e) { e.printStackTrace(); } return true; } /** * 复制文件 * * @param source * 源文件 * @param destination * 目标文件 */ public static void copyFile(File source, File destination) { FileInputStream sourceFile = null; FileOutputStream destinationFile = null; try { destination.createNewFile(); sourceFile = new FileInputStream(source); destinationFile = new FileOutputStream(destination); BufferedReader br = new BufferedReader(new FileReader(source)); // ByteArrayInputStream bin=new ByteArrayInputStream(br.r) BufferedWriter bw = new BufferedWriter(new FileWriter(destination)); String str = null; while ((str = br.readLine()) != null) { bw.write(str); bw.newLine(); bw.flush(); } } catch (FileNotFoundException f) { } catch (IOException e) { } finally { try { sourceFile.close(); } catch (Exception e) { } try { destinationFile.close(); } catch (Exception e) { } } } public static void copyImage(File source, File destination) { FileInputStream fi = null; try { fi = new FileInputStream(source); } catch (FileNotFoundException e) { e.printStackTrace(); } BufferedInputStream in = new BufferedInputStream(fi); FileOutputStream fo = null; try { fo = new FileOutputStream(destination); } catch (FileNotFoundException e) { e.printStackTrace(); } BufferedOutputStream out = new BufferedOutputStream(fo); byte[] buf = new byte[1024]; int len; try { len = in.read(buf); while (len != -1) { out.write(buf, 0, len); len = in.read(buf); } out.close(); fo.close(); in.close(); fi.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { // File originalImage = new File("C:\\11.jpg"); // resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f); // resize(originalImage, new File("c:\\11-1.jpg"),150, 1f); // File originalImage = new File("E:/1.jpg"); // resize(originalImage, new File("E:/1203-0.png"), 500, 0.7f); // resize(originalImage, new File("E:/1203-1.png"), 500, 1f); long time = System.currentTimeMillis(); process("E:/1.jpg"); System.out.println(System.currentTimeMillis() - time); // copyImage(new File("E:/Post.java"), new File("E:/Post2.java")); // copyImage("E:/1.jpg","E:/1_0.jpg"); } }
在网上找了找相关的资料,发现有个开源的框架jmagick.org,http://www.jmagick.org/lenya/jmagick/live/download.html,正在验证中……
发表评论
-
Java枚举的应用
2013-10-23 22:38 1323DK1.5引入了新的类型——枚举。在 Java 中它虽然算个 ... -
Java基础 之软引用、弱引用、虚引用
2013-09-28 19:55 1061在JDK1.2以前的版本中,当一个对象不被任何变 ... -
java接口与抽象类
2013-02-05 16:24 875抽象类:不能实例化,用来给子类继承,而具体类是不能被继承的 ... -
Eclipse 保存Java文件时自动格式化代码和去除不必要Import
2012-11-15 13:30 1129Eclipse中format代码的快捷方式是ctrl+s ... -
设计模式6大原则:里氏置换原则
2012-06-23 23:31 1052里氏置换原则(Liskov Substitution Prin ... -
为什么要使用多态?
2012-06-22 11:21 28961、多态的体现父类的引用指向自己的子类对象。父类的引用页可以接 ... -
Java中instanceof的用法详解
2012-06-17 12:27 22565interface A{ } class B imp ... -
xml解析方式:dom,sax,pull
2012-06-17 10:09 0现在看看一个简单的xml文件: <?xml ve ... -
Eclipse重构详解
2012-06-16 13:38 1213重构是对软件内部结构 ... -
ImageMagick & JMagick的使用-高品质高效率缩略图的解决方案
2012-06-04 14:41 2765Pure java的图片操作, ... -
java高质量图片缩放
2012-06-04 13:12 1257/** * 图片缩放 * @param ori ... -
java复制文件,复制图片不能打开解决。
2012-06-04 13:08 2545/** * 复制文件 * @param sourc ...
相关推荐
java图片裁剪技术
使用java代码,实现图片的裁剪,可以将图片按照要求进行裁剪。
java 对图片操作的工具类, 对图片进行剪裁 重置图片大小 按倍率缩小图片 对图片裁剪,并把裁剪新图片保存
一共包含了三种方法对于图片尺寸的修改———— 一种是使用第三方工具 ImageMagick 两种使用java自带类进行修改。
在JAVA编程里,实现图片的裁剪。java 图片裁剪 裁剪图片。
java上传并裁剪图片,功能已经实现,非常完美
java图片上传、控制大小、宽度高度和图片裁剪并保存
java裁剪、缩放图片工具类。含工具类和一个测试类。
自己写的一个java实现的图片裁剪源码,一个简单实现类,简单实用。
对图片裁剪,并把裁剪完新图片保存 描述如何对流进行解码的类<p>.用于指定如何在输入时从 Java Image I/O
Java实现图片裁剪预览, 详细可以看我的博文
java图片等比压缩后裁剪
19年10月3号更新 新资源地址https://download.csdn.net/download/b379685397/11831772。 使用教程见https://blog.csdn.net/b379685397/article/details/101940373 有问题可以私信我哦
jquery crop+java 裁剪上传图片,对图片缩放、移动、旋转、裁剪 上传到服务器。
这是一个java生成随即图片验证码,java图片压缩,java图片水印 ,java图片裁剪 ,java图片旋转 ,java图片放大缩小等工具类,里面有测试方法,欢迎使用!
java 处理图片大小等比例缩放;图片裁剪;支持图片压缩不失真;支持图片添加水印;支持动态图的处理;支持自定义图片大小的转换 等 压缩包中内附使用说明
用于切图的JAVA工程源码,可以根据具体的图片坐标和长和宽提图片中的某一部分。
java操作图片的工具类,包括图片放大缩小、图片裁剪、打水印(文字水印和图片水印)
NULL 博文链接:https://mutourenoo.iteye.com/blog/1249555