图片压缩
/** * 图片压缩。 * @param formatWideth 图片宽度 * @param formatHeight 图片高度 * @param fpath 临时存放路径 * @param filename 源图片名 * @param out 压缩后的图片名 * @param texts图片的后缀名 * @return FileInputStream 对象 * @throws PhotoManageExceptionPhotoManageException */ public static byte[] jMagicCompress(float formatWideth, float formatHeight, String fpath, String filename, String out, String texts) throws PhotoManageException { RunLogger.info("FileInputStream start"); byte[] bytes = null; //源图片的完整路径 String imgpath = fpath + filename; File file = new File(imgpath); BufferedImage bis = null; try { bis = ImageIO.read(file); if (null == bis) { throw new PhotoManageException(); } bis.flush(); int imageWideth = bis.getWidth(); int imageHeight = bis.getHeight(); if (imageWideth > formatWideth || imageHeight > formatHeight) { bytes = resize(formatWideth, formatHeight, imageWideth, imageHeight, imgpath); } else { bytes = getByteFromFile(file); } // bis.getAlphaRaster().getDataBuffer(). } catch (IOException e1) { RunLogger.error("BuffredImage read failed", e1); } return bytes; }/** * 按比例压缩图片 * @param formatWideth 压缩后的图片宽度 * @param formatHeight 压缩后的图片高度 * @param imageWideth 图片的真实宽度 * @param imageHeight 图片的真实高度 * @param oldImgPath * @return byte[] 返回图片的字节数组 */ private static byte[] resize(float formatWideth, float formatHeight, int imageWideth, int imageHeight, String oldImgPath) { byte[] bytes = null; // 宽度比例 float prw = imageWideth / formatWideth; // 高度比例 float prh = imageHeight / formatHeight; // 压缩比例 float pr = prh; if (formatHeight == PortalConstants.SMALLFORMATWIDTH) { if (prw < prh) { pr = prw; //按比例小的压缩 } } else { if (prw > prh) { pr = prw; //按比例大的压缩 } } if (pr < 1) { pr = 1; } String s1 = String.valueOf(imageWideth / pr); String s2 = String.valueOf(imageHeight / pr); int nw = Integer.parseInt(s1.substring(0, s1.indexOf("."))); int nh = Integer.parseInt(s2.substring(0, s2.indexOf("."))); if (nw < 1 || nh < 1) { nw = 1; nh = 1; } try { bytes = PhotoManageBean.getInstance().resize(oldImgPath, nw, nh); } catch (Exception e) { RunLogger.error("RESIZE METHODEXCEPTION", e); } return bytes; }/** * 图片管理。 * @authoralvin * @version[版本号, May 8, 2009] * @see[相关类/方法] * @sinceDPFV100R001C01B010 */public final class PhotoManageBean{ private static PhotoManageBean instance = new PhotoManageBean(); private final int half = 2; private PhotoManageBean() { } public static PhotoManageBean getInstance() { return instance; }/** * 图片压缩 * @param fromFileStr 源文件路径 * @param changeToWideth 压缩后的文件宽度 * @param changeToHeight 压缩后的文件高度 * @return boolean 值 * @see [类、类#方法、类#成员] */ public byte[] resize(String fromFileStr, int changeToWideth, int changeToHeight) { RunLogger.info("resize start"); MagickImage image = null; //ByteArrayOutputStream os = null; byte[] outblob = null; try { System.setProperty("jmagick.systemclassloader", "no"); ImageInfo info; DebugLogger.inf("Image file:" + fromFileStr, null); info = new ImageInfo(fromFileStr); image = new MagickImage(info); MagickImage scaled = image.scaleImage(changeToWideth, changeToHeight);//小图片文件的大小. // scaled.setFileName(saveToFileStr); // scaled.writeImage(info); outblob = scaled.imageToBlob(new ImageInfo()); //DebugLogger.inf("===================== " + outblob); // byte[] outblob = scaled.imageToBlob(info); DebugLogger.inf("Image size after compress:width=" + changeToWideth + ",height=" + changeToHeight, null); // os = new ByteArrayOutputStream(); // os.write(outblob); // os.flush(); // } catch (MagickException e) { RunLogger.error("File resize MagickException", e); } catch (Exception e) { RunLogger.error("File resize abnormity", e); } finally { if (null != image) { image.destroyImages(); image = null; } } return outblob; }}
页:
[1]