use of com.sun.image.codec.jpeg.JPEGImageEncoder in project Java8 by huhuhuHR.
the class OperateImage method mergeBothImageTopleftcorner.
/**
* 将附加图片合并到底图的左上角
*
* @param negativeImagePath
* 底图路径
* @param additionImagePath
* 附加图片路径
* @param toPath
* 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageTopleftcorner(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, 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();
}
}
}
use of com.sun.image.codec.jpeg.JPEGImageEncoder in project Java8 by huhuhuHR.
the class OperateImage method mergeBothImageToprightcorner.
/**
* 将附加图片合并到底图的右上角
*
* @param negativeImagePath
* 底图路径
* @param additionImagePath
* 附加图片路径
* @param toPath
* 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageToprightcorner(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() - image2.getWidth(), 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();
}
}
}
use of com.sun.image.codec.jpeg.JPEGImageEncoder in project Java8 by huhuhuHR.
the class OperateImage method enlargementImageEqualProportion.
/**
* 长高等比例放大图片
*
* @param srcImagePath
* 读取图形路径
* @param toImagePath
* 写入入行路径
* @param ratio
* 放大比例
* @throws IOException
*/
public void enlargementImageEqualProportion(String srcImagePath, String toImagePath, int ratio) throws IOException {
FileOutputStream out = null;
try {
// 读入文件
File file = new File(srcImagePath);
// 构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
// 放大边长
BufferedImage tag = new BufferedImage(width * ratio, height * ratio, BufferedImage.TYPE_INT_RGB);
// 绘制放大后的图片
tag.getGraphics().drawImage(src, 0, 0, width * ratio, height * ratio, 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 reduceImageEqualProportion.
/**
* 长高等比例缩小图片
*
* @param srcImagePath
* 读取图片路径
* @param toImagePath
* 写入图片路径
* @param ratio
* 缩小比例
* @throws IOException
*/
public void reduceImageEqualProportion(String srcImagePath, String toImagePath, int ratio) throws IOException {
FileOutputStream out = null;
try {
// 读入文件
File file = new File(srcImagePath);
// 构造Image对象
BufferedImage src = javax.imageio.ImageIO.read(file);
int width = src.getWidth();
int height = src.getHeight();
// 缩小边长
BufferedImage tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB);
// 绘制 缩小 后的图片
tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null);
out = new FileOutputStream(toImagePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
Aggregations