자바 그림 에 워 터 마크 (그림 또는 텍스트)
import org.springframework.util.StringUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* @author jasonLu
* @date 2017/5/11 12:30
* @Description:
*/
public class WaterMaskImgUtils
{
/**
*
*
* @param targetImg , :C://myPictrue//1.jpg
* @param waterImg , :C://myPictrue//logo.png
* @param x , x<0,
* @param y , y<0,
* @param alpha (0.0 -- 1.0, 0.0 ,1.0 )
* @param degree
*/
public static void pressImage(String targetImg, String waterImg, int x, int y, float alpha, Integer degree,String suffix)
{
pressImage(targetImg,waterImg,null,x,y,alpha,null,suffix);
}
/**
*
* @param targetImg , :C://myPictrue//1.jpg
* @param waterImg , :C://myPictrue//logo.png
* @param outImg , ,
* @param x , x<0,
* @param y , y<0,
* @param alpha (0.0 -- 1.0, 0.0 ,1.0 )
* @param degree
*/
public static void pressImage(String targetImg, String waterImg, String outImg, int x, int y, float alpha, Integer degree,String suffix)
{
try
{
File file = new File(targetImg);
// ,
File outFile;
if(StringUtils.isEmpty(outImg))
{
outFile = file;
}
else
{
outFile = new File(outImg);
}
Image image = null;
try
{
image = ImageIO.read(file);
}
catch (IOException e)
{
e.printStackTrace();
}
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);
if (null != degree)
{
//
g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
}
Image waterImage = ImageIO.read(new File(waterImg)); //
int width_1 = waterImage.getWidth(null);
int height_1 = waterImage.getHeight(null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int widthDiff = width - width_1;
int heightDiff = height - height_1;
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.drawImage(waterImage, x, y, width_1, height_1, null); //
g.dispose();
// .
ImageIO.write(bufferedImage, suffix.substring(1), outFile);
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
*
*
* @param targetImg , :C://myPictrue//1.jpg
* @param pressText , :
* @param fontName , :
* @param fontStyle , : (Font.BOLD|Font.ITALIC)
* @param fontSize ,
* @param color
* @param x , x<0,
* @param y , 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,String suffix)
{
pressText(targetImg,pressText,null,fontName,fontStyle,fontSize,color,x,y,alpha,null,suffix);
}
/**
*
* @Description (0,0) ,( , ) ,(0,xxx) ,(x,0) ,
* @param targetImg , :C://myPictrue//1.jpg
* @param pressText , :
* @param outImg , ,
* @param fontName , :
* @param fontStyle , : (Font.BOLD|Font.ITALIC)
* @param fontSize ,
* @param color
* @param positionX , x<0,
* @param positionY , y<0,
* @param alpha (0.0 -- 1.0, 0.0 ,1.0 )
* @param degree
*/
public static void pressText(String targetImg, String pressText, String outImg, String fontName, int fontStyle, int fontSize, Color color, int positionX, int positionY, float alpha, Integer degree,String suffix)
{
try
{
File file = new File(targetImg);
// ,
File outFile;
if (StringUtils.isEmpty(outImg))
{
outFile = file;
}
else
{
outFile = new File(outImg);
}
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);
Font font = new Font(fontName, fontStyle, fontSize);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.setFont(font);
g.setColor(color);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
if (null != degree)
{
//
g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
}
//
FontRenderContext context = g.getFontRenderContext();
Rectangle2D stringBounds = font.getStringBounds(pressText,context);
int textWidth = (int) stringBounds.getWidth() ;
int textHeight = (int) stringBounds.getHeight();
int widthDiff = width - textWidth;
int heightDiff = height - textHeight;
if (positionX < 0)
{
positionX = widthDiff / 2;
}
else if (positionX > widthDiff)
{
positionX = widthDiff;
}
if (positionY < 0)
{
positionY = heightDiff / 2;
}
else if (positionY > heightDiff)
{
positionY = heightDiff;
}
g.drawString(pressText, positionX, positionY + textHeight);
g.dispose();
ImageIO.write(bufferedImage, suffix, outFile);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
*
* @param targetImg , :C://myPictrue//1.jpg
* @param pressText , :
* @param fontName , :
* @param fontStyle , : (Font.BOLD|Font.ITALIC)
* @param fontSize ,
* @param alpha (0.0 -- 1.0, 0.0 ,1.0 )
*/
public static void pressTextToRightBottom(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, float alpha,String suffix)
{
pressTextToRightBottom(targetImg,pressText,null,fontName,fontStyle,fontSize,color,alpha,null,suffix);
}
/**
*
*
* @param targetImg , :C://myPictrue//1.jpg
* @param pressText , :
* @param outImg , ,
* @param fontName , :
* @param fontStyle , : (Font.BOLD|Font.ITALIC)
* @param fontSize ,
* @param alpha (0.0 -- 1.0, 0.0 ,1.0 )
* @param degree
*/
public static void pressTextToRightBottom(String targetImg, String pressText, String outImg, String fontName, int fontStyle, int fontSize, Color color, float alpha, Integer degree,String suffix)
{
try
{
File file = new File(targetImg);
File outFile;
if (StringUtils.isEmpty(outImg))
{
outFile = file;
}
else
{
outFile = new File(outImg);
}
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);
Font font = new Font(fontName, fontStyle, fontSize);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.setFont(font);
g.setColor(color);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
if (null != degree)
{
//
g.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
}
//
FontRenderContext context = g.getFontRenderContext();
Rectangle2D stringBounds = font.getStringBounds(pressText,context);
int textWidth = (int) stringBounds.getWidth() ;
int textHight = (int) stringBounds.getHeight();
g.drawString(pressText, width - textWidth, height - textHight);
g.dispose();
ImageIO.write(bufferedImage, suffix.substring(1), outFile);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.