use of com.sun.image.codec.jpeg.JPEGImageEncoder in project my_curd by qinyou.
the class ImageWatermark method pressText.
/**
* 打印文字水印图片,右下角计算坐标
*
* @param pressText --文字
* @param targetImg -- 目标图片
* @param fontName -- 字体名
* @param fontStyle -- 字体样式
* @param color -- 字体颜色
* @param fontSize -- 字体大小
* @param x -- 偏移量
* @param y
*/
public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, int color, int fontSize, int x, int y) {
try {
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
g.setColor(Color.RED);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.drawString(pressText, wideth - fontSize - x, height - fontSize / 2 - y);
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
use of com.sun.image.codec.jpeg.JPEGImageEncoder in project Java8 by huhuhuHR.
the class OperateImage method resizeImage.
/**
* 重置图形的边长大小
*
* @param srcImagePath
* @param toImagePath
* @param width
* @param height
* @throws IOException
*/
public void resizeImage(String srcImagePath, String toImagePath, int width, int height) throws IOException {
FileOutputStream out = null;
try {
// 读入文件
File file = new File(srcImagePath);
// 构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
// 放大边长
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 绘制放大后的图片
tag.getGraphics().drawImage(src, 0, 0, width, height, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
use of com.sun.image.codec.jpeg.JPEGImageEncoder in project Java8 by huhuhuHR.
the class OperateImage method mergeBothImageLeftcenter.
/**
* 将附加图片合并到底图的左边中央
*
* @param negativeImagePath
* 底图路径
* @param additionImagePath
* 附加图片路径
* @param toPath
* 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageLeftcenter(String negativeImagePath, String additionImagePath, String toPath) throws IOException {
InputStream is = null;
InputStream is2 = null;
OutputStream os = null;
try {
is = new FileInputStream(negativeImagePath);
is2 = new FileInputStream(additionImagePath);
BufferedImage image = ImageIO.read(is);
BufferedImage image2 = ImageIO.read(is2);
Graphics g = image.getGraphics();
g.drawImage(image2, 0, image.getHeight() / 2 - image2.getHeight() / 2, null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
if (is2 != null) {
is2.close();
}
if (is != null) {
is.close();
}
}
}
use of com.sun.image.codec.jpeg.JPEGImageEncoder in project Java8 by huhuhuHR.
the class OperateImage method mergeBothImageBottomcenter.
/**
* 将附加图片合并到底图的下边中央
*
* @param negativeImagePath
* 底图路径
* @param additionImagePath
* 附加图片路径
* @param toPath
* 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageBottomcenter(String negativeImagePath, String additionImagePath, String toPath) throws IOException {
InputStream is = null;
InputStream is2 = null;
OutputStream os = null;
try {
is = new FileInputStream(negativeImagePath);
is2 = new FileInputStream(additionImagePath);
BufferedImage image = ImageIO.read(is);
BufferedImage image2 = ImageIO.read(is2);
Graphics g = image.getGraphics();
g.drawImage(image2, image.getWidth() / 2 - image2.getWidth() / 2, image.getHeight() - image2.getHeight(), null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
if (is2 != null) {
is2.close();
}
if (is != null) {
is.close();
}
}
}
use of com.sun.image.codec.jpeg.JPEGImageEncoder in project Java8 by huhuhuHR.
the class OperateImage method mergeBothImageTopcenter.
/**
* 将附加图片合并到底图的上边中央
*
* @param negativeImagePath
* 底图路径
* @param additionImagePath
* 附加图片路径
* @param toPath
* 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageTopcenter(String negativeImagePath, String additionImagePath, String toPath) throws IOException {
InputStream is = null;
InputStream is2 = null;
OutputStream os = null;
try {
is = new FileInputStream(negativeImagePath);
is2 = new FileInputStream(additionImagePath);
BufferedImage image = ImageIO.read(is);
BufferedImage image2 = ImageIO.read(is2);
Graphics g = image.getGraphics();
g.drawImage(image2, image.getWidth() / 2 - image2.getWidth() / 2, 0, null);
os = new FileOutputStream(toPath);
JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(os);
enc.encode(image);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
if (is2 != null) {
is2.close();
}
if (is != null) {
is.close();
}
}
}
Aggregations