package com.ylcloud.common.util; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; /** * @Author FengGang * @Description TODO * @@date 2019/6/15 12:24 */ public class ImgUtil { public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); //创建一个Buffer字符串 byte[] buffer = new byte[1024]; //每次读取的字符串长度,如果为-1,代表全部读取完毕 int len = 0; //使用一个输入流从buffer里把数据读取出来 while ((len = inStream.read(buffer)) != -1) { //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 outStream.write(buffer, 0, len); } //关闭输入流 inStream.close(); //把outStream里的数据写入内存 return outStream.toByteArray(); } public static String getImgs(String url,String time) throws Exception { //new一个URL对象 URL urls = new URL(url); //打开链接 HttpURLConnection conn = (HttpURLConnection) urls.openConnection(); //设置请求方式为"GET" conn.setRequestMethod("GET"); //超时响应时间为5秒 conn.setConnectTimeout(5 * 1000); //通过输入流获取图片数据 InputStream inStream = conn.getInputStream(); //得到图片的二进制数据,以二进制封装得到数据,具有通用性 byte[] data = readInputStream(inStream); //new一个文件对象用来保存图片,默认保存当前工程根目录 String uuid = UUIDUtil.generateShortUuid(); File imageFile = new File(uuid+".jpg"); //创建输出流 FileOutputStream outStream = new FileOutputStream(imageFile); //写入数据 outStream.write(data); //关闭输出流 outStream.close(); //pressText("F:/图片/1.jpg", "________________", "宋体", Font.BOLD, 56, Color.black, 2020, 65, 1f); pressText(imageFile.getPath(), time, "宋体", Font.BOLD, 56, Color.white, 2020, 65, 1f); //pressText("F:/图片/2.jpg", "2019-06-06 12:12:12", "宋体", Font.BOLD, 56, Color.white, 65, 65, 1f); return "120.79.190.226:7002/"+imageFile.getPath(); } /** * * * 添加文字水印 * * @param targetImg 目标图片路径,如:C://myPictrue//1.jpg * * @param pressText 水印文字, 如:中国证券网 * * @param fontName 字体名称, 如:宋体 * * @param fontStyle 字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC) * * @param fontSize 字体大小,单位为像素 * * @param color 字体颜色 * * @param x 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间 * * @param y 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间 * * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) * */ public static void pressText(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, int x, int y, float alpha) { try { File file = new File(targetImg); Image image = ImageIO.read(file); int width = image.getWidth(null); int height = image.getHeight(null); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bufferedImage.createGraphics(); g.drawImage(image, 0, 0, width, height, null); g.setFont(new Font(fontName, fontStyle, fontSize)); g.setColor(color); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); int width_wi = fontSize * getTextLength(pressText); int height_wi = fontSize; int widthDiff = width - width_wi; int heightDiff = height - height_wi; if (x < 0) { x = widthDiff / 2; } else if (x > widthDiff) { x = widthDiff; } if (y < 0) { y = heightDiff / 2; } else if (y > heightDiff) { y = heightDiff; } g.drawString(pressText, x, y + height_wi);//水印文件 g.dispose(); ImageIO.write(bufferedImage, "JPEG", file); } catch (IOException e) { e.printStackTrace(); } } /** * * * 计算文字像素长度 * * @param text * * @return * */ private static int getTextLength(String text) { int textLength = text.length(); int length = textLength; for (int i = 0; i < textLength; i++) { int wordLength = String.valueOf(text.charAt(i)).getBytes().length; if (wordLength > 1) { length += (wordLength - 1); } } return length % 2 == 0 ? length / 2 : length / 2 + 1; } /* * 圆角处理 * @param BufferedImage * @param cornerRadius * */ public static String makeRoundedCorner(String srcImageFile, String result, String type, int cornerRadius) { try { BufferedImage image = ImageIO.read(new File(srcImageFile)); // int w = image.getWidth(); // int h = image.getHeight(); int w = image.getWidth(); int h = image.getHeight(); BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = output.createGraphics(); output = g2.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT); g2.dispose(); g2 = output.createGraphics(); //这里绘画圆角矩形 // g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // g2.fillRoundRect(0, 0,w, h, cornerRadius, cornerRadius); // g2.setComposite(AlphaComposite.SrcIn); //这里绘画原型图 Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, w, h); g2.setClip(shape); g2.drawImage(image, 0, 0, w, h, null); g2.dispose(); ImageIO.write(output, type, new File(result)); return result; } catch (IOException e) { e.printStackTrace(); } return null; } }
图片处理
最新推荐文章于 2022-11-18 19:14:26 发布