com.sun.image.codec.jpeg.JPEGImageEncoder Java Examples
The following examples show how to use
com.sun.image.codec.jpeg.JPEGImageEncoder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ScaleImageUtils.java From FlyCms with MIT License | 6 votes |
/** * <b>function:</b> 将Image的宽度、高度缩放到指定width、height,并保存在savePath目录 * @author hoojo * @createDate 2012-2-6 下午04:54:35 * @param width 缩放的宽度 * @param height 缩放的高度 * @param savePath 保存目录 * @param targetImage 即将缩放的目标图片 * @return 图片保存路径、名称 * @throws IOException */ public static String resize(int width, int height, String savePath, Image targetImage) throws IOException { width = Math.max(width, 1); height = Math.max(height, 1); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.getGraphics().drawImage(targetImage, 0, 0, width, height, null); if (savePath == null || "".equals(savePath)) { savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT; } FileOutputStream fos = new FileOutputStream(new File(savePath)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos); encoder.encode(image); image.flush(); fos.flush(); fos.close(); return savePath; }
Example #2
Source File: ScaleImageUtils.java From FlyCms with MIT License | 5 votes |
/** * <b>function:</b> 可以设置图片缩放质量,并且可以根据指定的宽高缩放图片 * @author hoojo * @createDate 2012-2-7 上午11:01:27 * @param width 缩放的宽度 * @param height 缩放的高度 * @param quality 图片压缩质量,最大值是1; 使用枚举值:{@link ImageQuality} * Some guidelines: 0.75 high quality、0.5 medium quality、0.25 low quality * @param savePath 保存目录 * @param targetImage 即将缩放的目标图片 * @return 图片保存路径、名称 * @throws IOException */ public static String resize(int width, int height, Float quality, String savePath, Image targetImage) throws IOException { width = Math.max(width, 1); height = Math.max(height, 1); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.getGraphics().drawImage(targetImage, 0, 0, width, height, null); if (savePath == null || "".equals(savePath)) { savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT; } FileOutputStream fos = new FileOutputStream(new File(savePath)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos); JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(image); if (quality == null || quality <= 0) { quality = DEFAULT_SCALE_QUALITY; } /** 设置图片压缩质量 */ encodeParam.setQuality(quality, true); encoder.encode(image, encodeParam); image.flush(); fos.flush(); fos.close(); return savePath; }
Example #3
Source File: ImageCache.java From xyTalk-pc with GNU Affero General Public License v3.0 | 5 votes |
/** * 生成图片缩略图 * * @param image * @param identify */ public void createThumb(Image image, String identify) { try { int[] imageSize = getImageSize(image); int destWidth = imageSize[0]; int destHeight = imageSize[1]; float scale = imageSize[0] * 1.0F / imageSize[1]; if (imageSize[0] > imageSize[1] && imageSize[0] > 200) { destWidth = 200; destHeight = (int) (destWidth / scale); } else if (imageSize[0] < imageSize[1] && imageSize[1] > 200) { destHeight = 200; destWidth = (int) (destHeight * scale); } // 开始读取文件并进行压缩 BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(image.getScaledInstance(destWidth, destHeight, Image.SCALE_SMOOTH), 0, 0, null); File cacheFile = new File(IMAGE_CACHE_ROOT_PATH + "/" + identify + "_thumb"); FileOutputStream out = new FileOutputStream(cacheFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } catch (IOException ex) { ex.printStackTrace(); } }
Example #4
Source File: ImageUtil.java From im4java-util with Artistic License 2.0 | 5 votes |
/** * jdk压缩图片-质量压缩 * * @param destImagePath 被压缩文件路径 * @param quality 压缩质量比例 * @return * @throws Exception */ public static void jdkResize(String destImagePath, float quality) throws Exception { // 目标文件 File resizedFile = new File(destImagePath); // 压缩 Image targetImage = ImageIO.read(resizedFile); int width = targetImage.getWidth(null); int height = targetImage.getHeight(null); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.createGraphics(); g.drawImage(targetImage, 0, 0, width, height, null); g.dispose(); String ext = getFileType(resizedFile.getName()); if (ext.equals("jpg") || ext.equals("jpeg")) { // 如果是jpg // jpeg格式的对输出质量进行设置 FileOutputStream out = new FileOutputStream(resizedFile); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(image); jep.setQuality(quality, false); encoder.setJPEGEncodeParam(jep); encoder.encode(image); out.close(); } else { ImageIO.write(image, ext, resizedFile); } }