java 压缩图片 添加水印

来源:互联网 发布:python在spss中的应用 编辑:程序博客网 时间:2024/06/05 15:53
/** * 压缩图片 *  * @param in *            图片流 * @param key *            文件ID * @param outputWidth *            输出图片宽度 * @param outputHeight *            输出图片高度 * @param zoom *            剪切方式 * @param water *            水印图片 * @param water_position *            水印位置 * @return */public byte[] compressPic(InputStream in, String key, int outputWidth, int outputHeight, String zoom, String water,String water_position) {try {Image img = ImageIO.read(in);// 获得源文件/* * 判断图片格式是否正确 */if (img != null && img.getWidth(null) != -1) {int newWidth;int newHeight;/* * 判断是否是等比缩放 */if (zoom != null && zoom.equals("1")) {/* * 为等比缩放计算输出的图片宽度及高度 */double rate1 = ((double) img.getWidth(null)) / (double) outputWidth;double rate2 = ((double) img.getHeight(null)) / (double) outputHeight;/* * 根据缩放比率大的进行缩放控制 */double rate = rate1 > rate2 ? rate1 : rate2;newWidth = (int) (((double) img.getWidth(null)) / rate);newHeight = (int) (((double) img.getHeight(null)) / rate);} else {newWidth = outputWidth; // 输出的图片宽度newHeight = outputHeight; // 输出的图片高度}BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);Graphics g = tag.createGraphics();// Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢g.drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);/* * 水印文件在原图片文件的位置,原图片文件的右下角为wideth-0,height-0 */if (water != null && !water.equals("0")) {addWatermark(g, water, newWidth, newHeight, water_position);}g.dispose();ByteArrayOutputStream out = new ByteArrayOutputStream();JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);// JPEGImageEncoder可适用于其他图片类型的转换encoder.encode(tag);out.close();byte[] outb = out.toByteArray();OutputStream fsOut = mogilefs.newFile(key, domain_class, out.size());fsOut.write(outb);in.close();fsOut.close();return outb;}} catch (IOException ex) {ex.printStackTrace();} catch (NoTrackersException e) {e.printStackTrace();} catch (TrackerCommunicationException e) {e.printStackTrace();} catch (StorageCommunicationException e) {e.printStackTrace();}return null;}

/** * 添加水印 *  * @param g * @param water *            (水印图片) * @param newWidth *            (原图的宽) * @param newHeight *            (原图的高) * @param water_position *            (水印位置) * @return */public boolean addWatermark(Graphics g, String water, int newWidth, int newHeight, String water_position) {try {/* * 根据参数不同获取不同的水印 */if (water.equals("1")) {InputStream is = mogilefs.getFileStream(water_pic);// 默认水印if (is != null) {Image image_water = ImageIO.read(is);int wideth_water = image_water.getWidth(null);int height_water = image_water.getHeight(null);if (water_position != null) {if (water_position.equals("0")) {// 无水印return true;} else if (water_position.equals("1")) {// 左上角g.drawImage(image_water, 0, 0, wideth_water, height_water, null);} else if (water_position.equals("2")) {// 右上角g.drawImage(image_water, newWidth - wideth_water, 0, wideth_water, height_water, null);} else if (water_position.equals("3")) {// 左下角g.drawImage(image_water, 0, newHeight - height_water, wideth_water, height_water, null);} else if (water_position.equals("4")) {// 右下角g.drawImage(image_water, newWidth - wideth_water, newHeight - height_water, wideth_water,height_water, null);} else if (water_position.equals("5")) {// 中间g.drawImage(image_water, newWidth / 2, newHeight / 2, wideth_water, height_water, null);} else if (water_position.equals("6")) {// 背景,暂时未实现} else if (water_position.equals("7")) {// 在左起0,高30%的地方加水印BigDecimal s = new BigDecimal(newHeight * 0.3).setScale(0, BigDecimal.ROUND_HALF_UP);g.drawImage(image_water, 0, (newHeight - s.intValue()), wideth_water, height_water, null);}}}} else {return false;}} catch (IOException e) {e.printStackTrace();} catch (NoTrackersException e) {e.printStackTrace();} catch (TrackerCommunicationException e) {e.printStackTrace();} catch (StorageCommunicationException e) {e.printStackTrace();}return true;}


0 0
原创粉丝点击