base64字符串加水印

package test; /** * 处理水印类 */ import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public final class ImageUtils { private static BASE64Encoder base64en = new BASE64Encoder(); private static BASE64Decoder base64de = new BASE64Decoder(); //水印图片base64码 private static String shuiyingImg = "水印的base64字符串,这里省略!" /** * 根据base64码加上水印后返回新的base64码 * @param srcStr 照片base64字符串 * @return */ public final static String addWatermark(String srcStr) throws IOException,Exception{ //根据传递的base64图片的大小来决定水印图片的大小。 //处理透明信息 float alpha = 0.3F; int srcWidth = 0; int srcHeight = 0; byte[] b ; try { b = base64de.decodeBuffer(srcStr); InputStream is = new java.io.ByteArrayInputStream(b); BufferedImage src = ImageIO.read(is); if(src != null ){ srcWidth = src.getWidth(null); srcHeight = src.getHeight(null); if(srcWidth<=0||srcHeight<=0) return null; //根据原始图片变换水印图片的尺寸 BufferedImage waterMark = resize(shuiyingImg,srcWidth,srcHeight); /*添加水印*/ BufferedImage img = new java.awt.image.BufferedImage(srcWidth, srcHeight,BufferedImage.TYPE_USHORT_565_RGB); //创建画板 Graphics2D graph = img.createGraphics(); //把原图印到图板上 graph.drawImage(src,null,0,0); //设置透明度,alpha graph.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); //画水印图片 graph.drawImage(waterMark,null,0,0); /*把图片转换为字节*/ ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jpegOutputStream); encoder.encode(img); byte[] resultByte = jpegOutputStream.toByteArray(); //ImageIO.write(img, "jpg",new File("d://wave.jpg")); //System.out.println("加水印完成"); graph.dispose(); //System.out.println(base64en.encode(resultByte)); return base64en.encode(resultByte); }else{ return null; } }catch (IOException e) { return null; } catch (Exception e) { return null; } } /** * 根据图片大小,自动变化水印图片大小。 * @param src: * @param w:原图片宽度 * @param h:元图片高度 * @return:返回image */ public static BufferedImage resize(String src,int w,int h) { byte[] b; // 加载内存中的水印图片 try { b = base64de.decodeBuffer(src); InputStream is = new java.io.ByteArrayInputStream(b); BufferedImage img = ImageIO.read(is); // 获得适合的缩放比率,即以在规定缩略尺寸中完整显示图片内容的同时又保证最大的缩放比率 // 根据比例画出缓存图像 BufferedImage mini = new java.awt.image.BufferedImage(w, h,BufferedImage.TYPE_USHORT_565_RGB); Graphics2D gmini = mini.createGraphics(); gmini.setBackground(Color.WHITE); // 让生成的图片按相同的比例变换 gmini.clearRect(0, 0, w, h); AffineTransform trans = new AffineTransform(); // 长和宽同时变换 trans.scale((double) w / img.getWidth(), (double) h/ img.getHeight()); gmini.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.7f)); AffineTransformOp op = new AffineTransformOp(trans,AffineTransformOp.TYPE_BILINEAR); gmini.drawImage(img, op, 0, 0); gmini.dispose(); return mini; } catch (IOException e) { return null; } } public static void main(String[] args) { String imgFile = "d://heart.jpg";//待处理的图片 InputStream in = null; byte[] data = null; //读取图片字节数组 try{ in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); }catch (IOException e){ e.printStackTrace(); } //对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); try { ImageUtils.addWatermark(encoder.encode(data)); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }

你可能感兴趣的:(exception,String,image,null,Graph,byte)