公司写的,不用了,放出来留个备忘。
final String IMG_FORMAT = "png_jpg_bmp_gif_tif_jpeg_PNG_JPG_BMP_GIF_TIF_JPEG";
final String IMG_FORMAT_NUMBER = "8950_ffd8_424d_4749_4d4d_4949";/*** 针对图片内容的格式效验。* 分别以:* 1:判断后缀名的方式判断是否为图片* 2:以魔术数字进行判断* 3:以imageIO流的方式验证是否为图片** @param mFile* @return boolean* @author lihao*/public boolean isImage(MultipartFile mFile) {File file = null;InputStream is = null;Image img = null;byte[] bt = new byte[2];try {file = ateTempFile("tmp", null);ansferTo(file);is = new FileInputStream(file);is.read(bt);img = ad(file);} catch (IOException e) {return false;}//获取文件后缀进行判断String suffix = OriginalFilename().OriginalFilename().lastIndexOf(".") + 1);if (IMG_FORMAT.indexOf(suffix) == -1) {return false;}//以魔术数字进行判断StringBuilder stringBuilder = new StringBuilder();for (int i = 0; i < bt.length; i++) {int v = bt[i] & 0xFF;//byte to intString hv = HexString(v);if (hv.length() < 2) {stringBuilder.append(0);}stringBuilder.append(hv);}System.out.String());if (IMG_FORMAT_NUMBER.String()) == -1) {return false;}//以imageIO流的方式对图片进行格式检查if (img == null || Width(null) <= 0 || Height(null) <= 0) {return false;}ists()){file.delete();}return true;}
下面是给图片添加水印, 适合用base64方式存的时候拿过来用。
/*** 给图片添加水印、可设置水印图片旋转角度** @param logoText 水印文字* @param degree 水印图片旋转角度* @param clarity 透明度(小于1的数)越接近0越透明* @param mFile 被操作的图片*/public byte[] waterMarkByText(String logoText,Integer degree,Float clarity, MultipartFile mFile) {// 图片流InputStream inputStream = null;ByteArrayOutputStream byteStream = null;Image srcImg = null;File file = null;byte[] byteImg = new byte[0];//水印添加位置坐标 1像素=1磅*DPI/72 显示屏dpi 大多是72 少数96 这里不对Integer width, height;Integer fontSize = 50;Integer rightButtomOffset = fontSize * 72 / 72 * 6;try {file = ateTempFile("tmp", null);ansferTo(file);srcImg = ad(file);width = Width(null) - rightButtomOffset;height = Height(null);BufferedImage buffImg = new Width(null),Height(null), BufferedImage.TYPE_INT_RGB);// 得到画笔对象Graphics2D g = ateGraphics();// 设置对线段的锯齿状边缘处理g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);g.Width(null),Height(null), Image.SCALE_SMOOTH), 0, 0,null);if (null != degree) {// 设置水印旋转g.Radians(degree),(double) Width() / 2,(double) Height() / 2);}// 设置颜色g.d);// 设置 Fontg.setFont(new Font("宋体", Font.BOLD, fontSize));float alpha = clarity;g.Instance(AlphaComposite.SRC_ATOP,alpha));// 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) .g.drawString(logoText, width, height);g.dispose();// 生成图片ImageIO.write(buffImg, "JPG", file);inputStream = new FileInputStream(file);byteStream = new ByteArrayOutputStream();int ch;while ((ch = ad()) != -1) {byteStream.write(ch);}byteImg = ByteArray();} catch (Exception e) {e.printStackTrace();} finally {try {if (null != inputStream) inputStream.close();if (null != byteStream) byteStream.close();if (ists()) {file.delete();}} catch (Exception e) {e.printStackTrace();}}return byteImg;}
本文发布于:2024-02-01 05:32:04,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170673672634246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |