有时候我们需要给照片上传的时候,加一些水印。
public String watermarkMulText(InputStream is, String filename, String realUploadPath) { String logoFileName = System.currentTimeMillis()+filename.substring(filename.lastIndexOf(".")); OutputStream os = null; try { Image image2 = ImageIO.read(is); int width = image2.getWidth(null); int height = image2.getHeight(null); // 1、创建缓存图片对象 BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); // 2、 创建JAVA绘图工具对象 Graphics2D graphics2D = bufferedImage.createGraphics(); // 3、使用绘图工具对象,将原图绘制到缓存图片对象 graphics2D.drawImage(image2,0,0,width,height,null); // 4、使用绘图工具,将水印(文字/图片)绘制到缓存图片对象 graphics2D.setFont(new Font(FONT_NAME,FONT_STYLE,FONT_SIZE)); graphics2D.setColor(FONT_COLOR); //S1-获取文字水印的宽、高 int width1 = FONT_SIZE*getTextLength(MARK_TEXT); int height1 = FONT_SIZE; graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,ALPHA));//设置水印图层 graphics2D.rotate(Math.toRadians(45),bufferedImage.getHeight()/2,bufferedImage.getHeight()/2);//图层以中心为基点,转30度的弧度。 int x = -width/2; int y = -height/2; while (x<width*1.5){ y = -height/2; while (y<height*1.5){ graphics2D.drawString(MARK_TEXT,x,y); y+=height1+MARGIN_ALL; } x+=width1+MARGIN_ALL; } graphics2D.dispose(); // 5、 创建图像编码工具类 String des = realUploadPath+File.separator+ logoFileName; os = new FileOutputStream(des); JPEGImageEncoder jpegImageEncoder = JPEGCodec.createJPEGEncoder(os); // 6、使用图片编码工具类,输出缓存图像到模板图片文件 jpegImageEncoder.encode(bufferedImage); } catch (Exception e) { e.printStackTrace(); } finally { if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } return realUploadPath+File.separator+logoFileName; }
以上修改于网上的一段代码,忘记保留出处了。
效果如下:
String --> InputStream
ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); InputStream --> String String inputStream2String(InputStream is){ BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null){ buffer.append(line); } return buffer.toString(); }
File --> InputStream
InputStream in = new FileInputStream(file);
InputStream --> File
public void inputstreamtofile(InputStream ins,File file){ OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close();
BufferedImage --> inputStream
BufferedImage --> String
public static InputStream encode(String content, String logoPath, boolean needCompress) throws Exception { BufferedImage image = QrCodeUtil.createImage(content, logoPath, true); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image, FORMAT, outputStream); //String streamEncoded = Base64Util.encode(outputStream.toByteArray()); InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); return inputStream; }
转:https://blog.csdn.net/weixin_39248420/article/details/89949880