| | |
| | | Ellipse2D.Double shapeEllipse = new Ellipse2D.Double(0, 0, Math.min(bi1.getWidth(),bi1.getHeight()), |
| | | Math.min(bi1.getWidth(),bi1.getHeight())); |
| | | |
| | | // 设置透明背景 |
| | | // 设置透明背景 |
| | | BufferedImage bi2 = new BufferedImage(bi1.getWidth(), |
| | | bi1.getHeight(), BufferedImage.TRANSLUCENT); |
| | | Graphics2D g2 = bi2.createGraphics(); |
| | | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| | | g2.setClip(shapeEllipse); |
| | | |
| | | // 画图片 |
| | | // 画图片 |
| | | g2.drawImage(bi1, 0, 0, null); |
| | | g2.dispose(); |
| | | |
| | | ImageIO.write(bi2, "PNG", file); |
| | | |
| | | } catch (IOException e) { |
| | | log.error("图片处理失败", e); |
| | | log.error("图片处理失败", e); |
| | | } |
| | | } |
| | | //矩形头像 |
| | | //矩形头像 |
| | | public static void doRectangle(File file){ |
| | | try { |
| | | BufferedImage bi1 = ImageIO.read(file); |
| | | Rectangle2D.Double shapeRectangle = new Rectangle2D.Double(0, 0, Math.min(bi1.getWidth(),bi1.getHeight()), |
| | | Math.min(bi1.getWidth(),bi1.getHeight())); |
| | | |
| | | // 设置透明背景 |
| | | // 设置透明背景 |
| | | BufferedImage bi2 = new BufferedImage(bi1.getWidth(), |
| | | bi1.getHeight(), BufferedImage.TRANSLUCENT); |
| | | Graphics2D g2 = bi2.createGraphics(); |
| | | g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| | | g2.setClip(shapeRectangle); |
| | | |
| | | // 画图片 |
| | | // 画图片 |
| | | g2.drawImage(bi1, 0, 0, null); |
| | | g2.dispose(); |
| | | |
| | | ImageIO.write(bi2, "PNG", file); |
| | | |
| | | } catch (IOException e) { |
| | | log.error("图片处理失败", e); |
| | | log.error("图片处理失败", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 为图片生成缩略图 |
| | | * 为图片生成缩略图 |
| | | * @param file |
| | | */ |
| | | public static void decreaseSizeImage(File file) { |
| | |
| | | int height = bi1.getHeight(); |
| | | |
| | | |
| | | // 设置透明背景 |
| | | // 设置透明背景 |
| | | BufferedImage bi2 = new BufferedImage(getPriviewImageWidth(width,height), |
| | | getPriviewImageHeight(width,height), bi1.getType()); |
| | | Graphics2D g2 = bi2.createGraphics(); |
| | | |
| | | // 画图片 |
| | | // 画图片 |
| | | Image image = bi1.getScaledInstance(getPriviewImageWidth(width,height), getPriviewImageHeight(width,height), Image.SCALE_DEFAULT); |
| | | g2.drawImage(image, 0, 0, null); |
| | | g2.dispose(); |
| | |
| | | ImageIO.write(bi2, "PNG", createPreviewFile(file)); |
| | | |
| | | } catch (IOException e) { |
| | | log.error("图片处理失败", e); |
| | | log.error("图片处理失败", e); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | public static String encodeImgageToBase64(String imgStr) { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 |
| | | public static String encodeImgageToBase64(String imgStr) { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 |
| | | |
| | | |
| | | ByteArrayOutputStream outputStream = null; |
| | |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | // 对字节数组Base64编码 |
| | | // 对字节数组Base64编码 |
| | | BASE64Encoder encoder = new BASE64Encoder(); |
| | | return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串 |
| | | return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串 |
| | | } |
| | | |
| | | public static void main(String[] args){ |