最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 實(shí)現(xiàn)圖片圓角處理、背景透明化

 更新時(shí)間:2021年11月15日 10:14:25   作者:孤獨(dú)地搬磚  
這篇文章主要介紹了java 實(shí)現(xiàn)圖片圓角處理、背景透明化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

java 圖片圓角處理、背景透明化

/**圖片圓角處理,背景透明化
     * @param srcImageFile 原圖片
     * @param result  處理后圖片
     * @param type   圖片格式
     * @param cornerRadius  720為圓角
     */
    public  void makeRoundedCorner(File srcImageFile, File result, String type, int cornerRadius) {        
        try {
            BufferedImage bi1 = ImageIO.read(srcImageFile);
            
            // 根據(jù)需要是否使用 BufferedImage.TYPE_INT_ARGB
            BufferedImage image = new BufferedImage(bi1.getWidth(), bi1.getHeight(),
                    BufferedImage.TYPE_INT_ARGB);
      
            Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1
                    .getHeight());
             
            Graphics2D g2 = image.createGraphics();
            image = g2.getDeviceConfiguration().createCompatibleImage(bi1.getWidth(), bi1.getHeight(), Transparency.TRANSLUCENT);
            g2 = image.createGraphics();
            g2.setComposite(AlphaComposite.Clear);
            g2.fill(new Rectangle(image.getWidth(), image.getHeight()));
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1.0f));
            g2.setClip(shape);
            // 使用 setRenderingHint 設(shè)置抗鋸齒
            g2 = image.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.fillRoundRect(0, 0,bi1.getWidth(), bi1.getHeight(), cornerRadius, cornerRadius);
            g2.setComposite(AlphaComposite.SrcIn);
            g2.drawImage(bi1, 0, 0, bi1.getWidth(), bi1.getHeight(), null);
            g2.dispose();
            ImageIO.write(image, type, result);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

java 的圖片處理解析

直接上效果圖,現(xiàn)在有的需求就是把用戶的頭像,跟昵稱嵌入到這個(gè)背景圖中。

第一步,把頭像切成圓角,背景透明的圖片。

第二部,把第一步生成的圖片,當(dāng)成水印放到坐標(biāo)的左邊的紅箭頭的地方

第三部,創(chuàng)建文字水印,然后放入到右邊的紅箭頭地方。

效果圖如下:

由于需要thumbnailator組件支持

先導(dǎo)入maven

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

直接上代碼:

package com.image;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Transparency;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
public class ImageDo {
public static void main(String[] args) throws IOException {
//首先獲取
makeRoundedCorner("C:/Users/luojie/Desktop/0.jpg", "C:/Users/luojie/Desktop/2.png", "png", 170);
//后續(xù)水印在背景圖片的的x軸y軸的坐標(biāo)
Position ab=  new Position() {
@Override
public Point calculate(int enclosingWidth, int enclosingHeight, int width, int height, int insetLeft,
int insetRight, int insetTop, int insetBottom) {
// TODO Auto-generated method stub
return new Point(89, 53);
}
};
//把生成的圓形圖片變換成寬高142x142的圖片
Thumbnails.of("C:/Users/luojie/Desktop/2.png").size(142, 142).toFile(
"C:/Users/luojie/Desktop/2_142x142.png");
//把生成的圓形圖片,當(dāng)水印貼到背景圖中,ab為圓形圖片應(yīng)該到背景圖的x軸y軸的坐標(biāo)        
Thumbnails.of("C:/Users/luojie/Desktop/cmbg.png").size(1280, 1024).watermark(ab,
ImageIO.read(new File("C:/Users/luojie/Desktop/2_142x142.png")), 1f)
.outputQuality(0.8f).toFile("C:/Users/luojie/Desktop/image_watermark_bottom_right.jpg");
//給文字水印
pressText("C:/Users/luojie/Desktop/image_watermark_bottom_right.jpg", "WEIXINYONGHU", "Comic Sans MS", Font.BOLD, 30, Color.BLACK,275, 65, 1f);  
}
    /**  
     * 添加文字水印  
     * @param targetImg 目標(biāo)圖片路徑,如:C://myPictrue//1.jpg  
     * @param pressText 水印文字, 如:中國(guó)證券網(wǎng)  
     * @param fontName 字體名稱,    如:宋體  
     * @param fontStyle 字體樣式,如:粗體和斜體(Font.BOLD|Font.ITALIC)  
     * @param fontSize 字體大小,單位為像素  
     * @param color 字體顏色  
     * @param x 水印文字距離目標(biāo)圖片左側(cè)的偏移量,如果x<0, 則在正中間  
     * @param y 水印文字距離目標(biāo)圖片上側(cè)的偏移量,如果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();  
        }  
    }      
    
    /** 
     * 計(jì)算文字像素長(zhǎng)度 
     * @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();
       //這里繪畫(huà)圓角矩形
//        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        g2.fillRoundRect(0, 0,w, h, cornerRadius, cornerRadius);
//        g2.setComposite(AlphaComposite.SrcIn);
       
       //這里繪畫(huà)原型圖
       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;
}
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java中抽象類、抽象方法、接口與實(shí)現(xiàn)接口實(shí)例詳解

    java中抽象類、抽象方法、接口與實(shí)現(xiàn)接口實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于java中抽象類、抽象方法、接口與實(shí)現(xiàn)接口的相關(guān)資料,文中通過(guò)示例代碼將四者介紹的非常詳細(xì),并且簡(jiǎn)單介紹了抽象類和接口的區(qū)別,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Java MyBatis返回兩個(gè)字段作為Map的key和value問(wèn)題

    Java MyBatis返回兩個(gè)字段作為Map的key和value問(wèn)題

    使用MyBatis查詢兩個(gè)字段并返回Map時(shí),需要注意數(shù)據(jù)量和值的類型,直接返回Map會(huì)導(dǎo)致報(bào)錯(cuò),使用@MapKey注解可以生成Map,但值是對(duì)象而不是直接值,為了解決這個(gè)問(wèn)題,可以自定義一個(gè)Map結(jié)果處理器MapResultHandler
    2024-12-12
  • java代碼關(guān)閉tomcat程序及出現(xiàn)問(wèn)題解析

    java代碼關(guān)閉tomcat程序及出現(xiàn)問(wèn)題解析

    這篇文章主要介紹了java代碼關(guān)閉tomcat程序 及出現(xiàn)問(wèn)題解析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2019-05-05
  • 解決Spring Security中AuthenticationEntryPoint不生效相關(guān)問(wèn)題

    解決Spring Security中AuthenticationEntryPoint不生效相關(guān)問(wèn)題

    這篇文章主要介紹了解決Spring Security中AuthenticationEntryPoint不生效相關(guān)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java編程用指定字符打印菱形實(shí)例

    Java編程用指定字符打印菱形實(shí)例

    本文主要介紹了用指定的字符打印菱形的方法實(shí)例,一個(gè)簡(jiǎn)單容日上手的小程序,喜歡的朋友可以拿來(lái)練習(xí)一下。
    2017-09-09
  • SpringBoot兩種方式刷新配置信息

    SpringBoot兩種方式刷新配置信息

    這篇文章主要介紹了SpringBoot兩種方式刷新配置信息,一種是@?ConfigurationProperties?不能自動(dòng)刷新,需要手動(dòng)調(diào)用contextRefresher.refresh()方法來(lái)刷新配置,第二種方法可以嘗試下,需要的朋友可以參考下
    2023-08-08
  • mybatisplus實(shí)現(xiàn)自動(dòng)填充時(shí)間的項(xiàng)目實(shí)踐

    mybatisplus實(shí)現(xiàn)自動(dòng)填充時(shí)間的項(xiàng)目實(shí)踐

    在數(shù)據(jù)庫(kù)操作中,頻繁設(shè)置創(chuàng)建時(shí)間和更新時(shí)間字段非常繁瑣,通過(guò)使用MyBatis-Plus的自動(dòng)填充功能,可以簡(jiǎn)化操作,本文就來(lái)詳細(xì)的介紹一下,感興趣的可以了解一下
    2024-10-10
  • Spring攔截器和過(guò)濾器的區(qū)別在哪?

    Spring攔截器和過(guò)濾器的區(qū)別在哪?

    相信很多小伙伴都對(duì)Spring攔截器和過(guò)濾器的區(qū)別有疑惑,今天特地整理了本篇文章,文中有非常詳細(xì)的介紹,需要的朋友可以參考下
    2021-06-06
  • java自定義注解接口實(shí)現(xiàn)方案

    java自定義注解接口實(shí)現(xiàn)方案

    java注解是附加在代碼中的一些元信息,用于一些工具在編譯、運(yùn)行時(shí)進(jìn)行解析和使用,起到說(shuō)明、配置的功能,本文將詳細(xì)介紹,此功能的實(shí)現(xiàn)方法
    2012-11-11
  • 通過(guò)實(shí)例了解Java jdk和jre的區(qū)別

    通過(guò)實(shí)例了解Java jdk和jre的區(qū)別

    這篇文章主要介紹了通過(guò)實(shí)例了解Java jdk和jre的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05

最新評(píng)論

务川| 乡宁县| 尼勒克县| 银川市| 嘉定区| 丽水市| 莱芜市| 兴义市| 灵石县| 洪洞县| 尼勒克县| 思茅市| 望谟县| 东兴市| 商城县| 驻马店市| 滕州市| 阿拉善左旗| 安平县| 囊谦县| 桃源县| 综艺| 安仁县| 宜黄县| 南郑县| 西充县| 太和县| 清河县| 日土县| 金湖县| 沈丘县| 繁峙县| 安远县| 六盘水市| 乌兰浩特市| 油尖旺区| 定边县| 安康市| 九龙城区| 陈巴尔虎旗| 平塘县|