use of com.sun.image.codec.jpeg.JPEGImageEncoder in project my_curd by qinyou.
the class ImageWatermark method pressImage.
/**
* 把图片印刷到图片上
*
* @param pressImg -- 水印文件
* @param targetImg -- 目标文件
* @param x
* @param y
*/
public static final void pressImage(String pressImg, String targetImg, 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);
// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, wideth - wideth_biao - x, height - height_biao - y, wideth_biao, height_biao, null);
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.sun.image.codec.jpeg.JPEGImageEncoder in project pancm_project by xuwujing.
the class CompressImage method resize.
/**
* 强制压缩/放大图片到固定的大小
*
* @param w int 新宽度
* @param h int 新高度
*/
public static void resize(int w, int h, String toPic, String fileName) throws IOException {
// 读入文件
File file = new File(fileName);
BufferedImage img = ImageIO.read(file);
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 绘制缩小后的图
image.getGraphics().drawImage(img, 0, 0, w, h, null);
File destFile = new File(toPic);
// 输出到文件流
FileOutputStream out = new FileOutputStream(destFile);
// 可以正常实现bmp、png、gif转jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
// JPEG编码
encoder.encode(image);
out.close();
}
use of com.sun.image.codec.jpeg.JPEGImageEncoder in project Java8 by huhuhuHR.
the class OperateImage method enlargementImageByRatio.
/**
* 按倍率放大图片
*
* @param srcImagePath
* 读取图形路径
* @param toImagePath
* 写入入行路径
* @param widthRatio
* 宽度放大比例
* @param heightRatio
* 高度放大比例
* @throws IOException
*/
public void enlargementImageByRatio(String srcImagePath, String toImagePath, int widthRatio, int heightRatio) 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 * widthRatio, height * heightRatio, BufferedImage.TYPE_INT_RGB);
// 绘制放大后的图片
tag.getGraphics().drawImage(src, 0, 0, width * widthRatio, height * heightRatio, 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 mergeBothImageLeftbottom.
/**
* 将附加图片合并到底图的左下角
*
* @param negativeImagePath
* 底图路径
* @param additionImagePath
* 附加图片路径
* @param toPath
* 合成图片写入路径
* @throws IOException
*/
public void mergeBothImageLeftbottom(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() - 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 reduceImageByRatio.
/**
* 按倍率缩小图片
*
* @param srcImagePath
* 读取图片路径
* @param toImagePath
* 写入图片路径
* @param widthRatio
* 宽度缩小比例
* @param heightRatio
* 高度缩小比例
* @throws IOException
*/
public void reduceImageByRatio(String srcImagePath, String toImagePath, int widthRatio, int heightRatio) 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 / widthRatio, height / heightRatio, BufferedImage.TYPE_INT_RGB);
// 绘制 缩小 后的图片
tag.getGraphics().drawImage(src, 0, 0, width / widthRatio, height / heightRatio, 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