zoukankan      html  css  js  c++  java
  • graphics 切圆背景透明

     public static String fileUploadL(MultipartFile file, String path){
            String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String fileName = System.currentTimeMillis()+extName;
            
            String finalPath = BASE_PATH+path;
            try { 
                logger.info("==========储存路径"+finalPath);
                //文件夹不存在则创建文件夹
                File filepath = new File(finalPath);
                if (!filepath.exists()) {
                    logger.info("==========创建文件夹");
                    filepath.mkdirs();
                }
                //文件保存路径  
                String savePath = filepath+ "/" + fileName; 
                logger.info("==========全路径"+savePath);
                //转存文件  
                //file.transferTo(new File(savePath));
                logger.info("==========访问路径"+ACCESS_PATH+path+fileName);
                //multipartFile转File
                CommonsMultipartFile cf= (CommonsMultipartFile)file; 
                DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
    
                File f = fi.getStoreLocation();
    
                BufferedImage bi1 = ImageIO.read(f);
                    // 根据需要是否使用 BufferedImage.TYPE_INT_ARGB

              //设置以最小的边截取 int num =0; if(bi1.getWidth()>bi1.getHeight()) { num = bi1.getHeight(); }else { num = bi1.getWidth(); } BufferedImage image = new BufferedImage(num,num, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, num, num); g2.setClip(shape); // 使用 setRenderingHint 设置抗锯齿 g2.drawImage(bi1, 0, 0, null); g2.dispose(); ImageIO.write(resize(100, 100, image), "PNG", new File(savePath)); return ACCESS_PATH+path+fileName; } catch (Exception e) { e.printStackTrace(); } return null; }
      //设置图片px
    public static BufferedImage resize(int targetWidth, int targetHeight, BufferedImage src) { double scaleW = (double) targetWidth / (double) src.getWidth() ; double scaleH = (double) targetHeight / (double) src.getHeight() ; double scale = scaleW < scaleH ? scaleW : scaleH; BufferedImage result = new BufferedImage((int) (src.getWidth() * scale), (int) (src.getHeight() * scale), BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = result.createGraphics(); g2d.drawImage(src, 0, 0, result.getWidth(), result.getHeight(), null); g2d.dispose(); return result; }
  • 相关阅读:
    hihocoder 1049 后序遍历
    hihocoder 1310 岛屿
    Leetcode 63. Unique Paths II
    Leetcode 62. Unique Paths
    Leetcode 70. Climbing Stairs
    poj 3544 Journey with Pigs
    Leetcode 338. Counting Bits
    Leetcode 136. Single Number
    Leetcode 342. Power of Four
    Leetcode 299. Bulls and Cows
  • 原文地址:https://www.cnblogs.com/yang1018/p/10178917.html
Copyright © 2011-2022 走看看