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

Java基礎(chǔ)之簡單的圖片處理

 更新時間:2021年04月30日 09:24:46   作者:朝如青絲·暮成雪  
這篇文章主要介紹了Java基礎(chǔ)之簡單的圖片處理,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下

一、前言

先使用一個模板圖片,在圖片上添加圖片或者文字都可以。

二、依賴

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.18</version>
    <optional>true</optional>
</dependency>

三、封裝數(shù)據(jù)類

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.awt.*;

/**
 * 坐標(biāo)數(shù)據(jù)
 * @author tyg
 * @date 2021-04-23 14:33
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PositionPO {

    /** 顯示的數(shù)據(jù) */
    private Object data;
    /** X軸坐標(biāo) */
    private float x;
    /** Y軸坐標(biāo) */
    private float y;
    /** 寬度 */
    private float w;
    /** 高度 */
    private float h;
    /** 字體 */
    private Font font;

    public PositionPO(Object data, float x, float y, float w, float h) {
        this.data = data;
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }

    public PositionPO(Object data, float x, float y) {
        this.data = data;
        this.x = x;
        this.y = y;
    }

    public PositionPO(Object data, float x, float y, Font font) {
        this.data = data;
        this.x = x;
        this.y = y;
        this.font = font;
    }

    public PositionPO(float x, float y, float w, float h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }
}
import com.yt.distributor.po.pdf.PositionPO;
import lombok.Data;

import java.util.List;

/**
 * 邀請海報
 * @author tyg
 * @date 2021-04-24 14:52
 */
@Data
public class ImageHandlePO {

    /** 文字 */
    private List<PositionPO> textList;
    /** 圖片 */
    private List<PositionPO> imageList;

    public ImageHandlePO(List<PositionPO> textList, List<PositionPO> imageList) {
        this.textList = textList;
        this.imageList = imageList;
    }
}

四、常量類

package com.yt.distributor.constant;

import org.springframework.core.io.ClassPathResource;

import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
 * 圖片常量
 * @author tyg
 * @date 2021-04-24 16:59
 */
public class ImageConstant {

    /** 透明度 */
    public static final float PELLUCIDITY = 1.0F;
    /** 字體 */
    public static final Font FONT = new Font("微軟雅黑", Font.BOLD, 18);
    /** 邀請海報模板圖片源文件 */
    public static File POSTER_SOURCE_FILE;
    /** 圖片默認(rèn)格式 */
    public static final String FORMAT = "png";

    static{
        try {
            ClassPathResource resource = new ClassPathResource("conf/poster.jpg");
            POSTER_SOURCE_FILE = resource.getFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

五、圖像處理類

import com.yt.distributor.constant.ImageConstant;
import com.yt.distributor.po.img.ImageHandlePO;
import com.yt.distributor.po.pdf.PositionPO;
import lombok.extern.log4j.Log4j2;
import net.dreamlu.mica.core.utils.$;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 圖像合成處理
  * 注:圖像處理的原點(diǎn)坐標(biāo)在:左上角,距離為像素
 * @author tyg
 * @date 2021-04-24 17:45
 */
@Log4j2
public class PictureSynthesis {

    /** 原模板圖片文件 */
    public static final Object FLAG = true;
    /** 原模板圖片文件 */
    public static File sourceFile;


    public static void main(String[] args) throws IOException {
        // 生成二維碼
        BufferedImage image = QrCodeGenerator.generateQrCode("http://www.baiud.com/index.html?id=13", 192, 192);
        // 圖片
        List<PositionPO> imageList = new ArrayList<>();
        imageList.add(new PositionPO(ImageIO.read(new URL("https://thirdwx.qlogo.cn/mmopen/vi_32/AtTHbmrMict69vB7ocDMbstibgvwxpK51bOoNkQiaemrImnicUK2L9OoF1JibHiceLwY53ibiaicJQibuEwLNFicJiaYcQHRiaw/132")), 120F, 1688F, 192F, 192F));
        imageList.add(new PositionPO(image, 785F, 1632F, 192F , 192F));

        // 文字
        Font font = new Font("微軟雅黑", Font.PLAIN, 30);
        List<PositionPO> textList = new ArrayList<>();
        textList.add(new PositionPO("顏魔子辰", 120F, 1660F, font));
        textList.add(new PositionPO("顏魔子辰邀請您", 336F, 1758F, font));
        textList.add(new PositionPO("加入某某小店。", 336F, 1796F, font));
        textList.add(new PositionPO("長按可識別二維碼", 760F, 1880F, font));

        String sourcePath = "C:\\Users\\Administrator\\Desktop\\poster.jpg";
        String savePath = "C:\\Users\\Administrator\\Desktop\\poster-handle.jpg";
        // 輸出水印圖片
        handleImage(new ImageHandlePO(textList, imageList), new File(sourcePath), savePath);
    }

    /**
     * 圖片處理(返回輸入流)
     * @param po            處理的數(shù)據(jù)
     * @author tyg
     * @date 2021-04-14 15:45
     * @return InputStream
     */
    public static InputStream handleImage(ImageHandlePO po, File sourceFile) throws IOException {
        synchronized (FLAG) {
            PictureSynthesis.sourceFile = sourceFile;
            //圖片處理,導(dǎo)出數(shù)據(jù)
            BufferedImage image = watermark(po);
            return getInputStream(image);
        }
    }

    /**
     * 圖片處理(輸出到文件中)
     * @param po            處理的數(shù)據(jù)
     * @param saveFilePath  保存的路徑
     * @author tyg
     * @date   2017年9月6日下午12:53:11
     */
    public static void handleImage(ImageHandlePO po, File sourceFile, String saveFilePath) throws IOException {
        synchronized (FLAG) {
            PictureSynthesis.sourceFile = sourceFile;
            // 構(gòu)建疊加層
            BufferedImage buffImg = watermark(po);
            // 輸出水印圖片
            generateWaterFile(buffImg, saveFilePath);
        }
    }

    /**
     * 構(gòu)建疊加層
     * 圖像處理的原點(diǎn)坐標(biāo)在:左上角
     * @param po 處理的數(shù)據(jù)
     * @throws IOException io異常
     * @return BufferedImage 生成水印并返回java.awt.image.BufferedImage
     */
    private static BufferedImage watermark(ImageHandlePO po) throws IOException {
        // 獲取底圖
        BufferedImage buffImg = ImageIO.read(sourceFile);
        // 創(chuàng)建Graphics2D對象,用在底圖對象上繪圖
        Graphics2D g2d = buffImg.createGraphics();

        // 處理文字
        if ($.isNotEmpty(po.getTextList())){
            for (PositionPO pp : po.getTextList()){
                g2d.setColor(Color.black);
                g2d.setFont( pp.getFont() == null ? ImageConstant.FONT : pp.getFont());
                g2d.drawString(pp.getData().toString(), pp.getX(), pp.getY());
            }
        }
        // 處理圖片
        if ($.isNotEmpty(po.getImageList())){
            for (PositionPO pp : po.getImageList()){
                BufferedImage image = (BufferedImage) pp.getData();
                // 獲取層圖的寬度
                int width = pp.getW() <= 0 ? image.getWidth() : (int) pp.getW();
                // 獲取層圖的高度
                int height = pp.getH() <= 0 ? image.getHeight() : (int) pp.getH();
                // 在圖形和圖像中實現(xiàn)混合和透明效果
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ImageConstant.PELLUCIDITY));
                // 繪制
                g2d.drawImage(image, (int)pp.getX(), (int)pp.getY(), width, height, null);
            }
        }
        // 釋放圖形上下文使用的系統(tǒng)資源
        g2d.dispose();
        return buffImg;
    }

    /**
     * 輸出水印圖片
     * @param buffImg  圖像加水印之后的BufferedImage對象
     * @param savePath 圖像加水印之后的保存路徑
     * @author tyg
     * @date 2021-04-24 16:19
     */
    private static void generateWaterFile(BufferedImage buffImg, String savePath) {
        int temp = savePath.lastIndexOf(".") + 1;
        try {
            ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 獲取系統(tǒng)所支持的字體
     * @author tyg
     * @date 2021-04-24 16:19
     */
    private static void getFonts(){
        String[] fontNames=GraphicsEnvironment.getLocalGraphicsEnvironment().
                getAvailableFontFamilyNames();
        for(String fontName:fontNames){
            System.out.println(fontName);
        }
    }

    /**
     * 獲取圖片輸入流
     * @param image	圖片
     * @author tyg
     * @date 2021-04-14 17:14
     * @return java.io.InputStream
     */
    public static InputStream getInputStream(BufferedImage image){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, ImageConstant.FORMAT, os);
            return new ByteArrayInputStream(os.toByteArray());
        } catch (IOException e) {
            log.error("提示:",e);
        }
        return null;
    }

}

六、效果圖

以上的數(shù)據(jù)都是按圖片的1080*1920像素來設(shè)定的,下面紅框部分是動態(tài)生成的。

在這里插入圖片描述

到此這篇關(guān)于Java基礎(chǔ)之簡單的圖片處理的文章就介紹到這了,更多相關(guān)Java圖片處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java超細(xì)致講解數(shù)組的使用

    Java超細(xì)致講解數(shù)組的使用

    數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲固定大小的同類型元素
    2022-05-05
  • 單點(diǎn)登錄的概念及SpringBoot實現(xiàn)單點(diǎn)登錄的操作方法

    單點(diǎn)登錄的概念及SpringBoot實現(xiàn)單點(diǎn)登錄的操作方法

    在本文中,我們將使用Spring Boot構(gòu)建一個基本的單點(diǎn)登錄系統(tǒng),我們將介紹如何使用Spring Security和JSON Web Tokens(JWTs)來實現(xiàn)單點(diǎn)登錄功能,本文假設(shè)您已經(jīng)熟悉Spring Boot和Spring Security,感興趣的朋友一起看看吧
    2024-10-10
  • Java使用過濾器防止SQL注入XSS腳本注入的實現(xiàn)

    Java使用過濾器防止SQL注入XSS腳本注入的實現(xiàn)

    這篇文章主要介紹了Java使用過濾器防止SQL注入XSS腳本注入,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Spring中的AOP原理與使用詳解

    Spring中的AOP原理與使用詳解

    這篇文章主要介紹了Spring中的AOP原理與使用詳解,AOP意為面向切面編程,可以通過預(yù)編譯方式或運(yùn)行期動態(tài)代理實現(xiàn)在不修改源代碼的情況下給程序動態(tài)統(tǒng)一添加功能的一種技術(shù),需要的朋友可以參考下
    2023-12-12
  • Spring-Cloud-Function-Spel?漏洞環(huán)境搭建

    Spring-Cloud-Function-Spel?漏洞環(huán)境搭建

    這篇文章主要介紹了Spring-Cloud-Function-Spel?漏洞復(fù)現(xiàn)及搭建方法,搭建方法也很簡單,首先需要安裝maven jdk,具體安裝過程跟隨小編一起看看吧
    2022-03-03
  • 如何解決executors線程池創(chuàng)建的線程不釋放的問題

    如何解決executors線程池創(chuàng)建的線程不釋放的問題

    這篇文章主要介紹了如何解決executors線程池創(chuàng)建的線程不釋放的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • JDK15正式發(fā)布(新增功能預(yù)覽)

    JDK15正式發(fā)布(新增功能預(yù)覽)

    這篇文章主要介紹了JDK15正式發(fā)布,新增功能預(yù)覽,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2020-09-09
  • Java經(jīng)典設(shè)計模式之策略模式原理與用法詳解

    Java經(jīng)典設(shè)計模式之策略模式原理與用法詳解

    這篇文章主要介紹了Java經(jīng)典設(shè)計模式之策略模式,簡單說明了策略模式的概念、原理并結(jié)合實例形式分析了java策略模式的具有用法與相關(guān)注意事項,需要的朋友可以參考下
    2017-08-08
  • Java注解簡介和使用詳細(xì)講解

    Java注解簡介和使用詳細(xì)講解

    Java 語言中的類、構(gòu)造器、方法、成員變量、參數(shù)等都可以被注解進(jìn)行標(biāo)注,這篇文章主要介紹了Java注解的介紹和使用詳細(xì)講解,需要的朋友可以參考下
    2023-02-02
  • Java中如何正確重寫equals方法

    Java中如何正確重寫equals方法

    Object類中equals方法比較的是兩個對象的引用地址,只有對象的引用地址指向同一個地址時,才認(rèn)為這兩個地址是相等的,否則這兩個對象就不相等
    2021-10-10

最新評論

安顺市| 安阳县| 涟源市| 普兰县| 凉山| 辽中县| 固始县| 富蕴县| 花莲市| 寻乌县| 陆河县| 三台县| 郁南县| 进贤县| 沂源县| 松溪县| 屯留县| 陆良县| 海丰县| 五大连池市| 余江县| 晋江市| 青河县| 寻乌县| 银川市| 吉林省| 昌都县| 广昌县| 雷山县| 大悟县| 辽源市| 子长县| 化州市| 东港市| 沐川县| 北川| 保康县| 太保市| 偃师市| 芮城县| 阆中市|