您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java如何实现图像处理和P图?

2024/4/4 6:32:51发布12次查看
准备阶段原图(来源于网络)
ps处理后的模板图
待合成图片
图片合成基本步骤读取模板图和待合成图,调整待合成图的尺寸、旋转角度、亮度、对比度;
创建一个与模板图尺寸一致的空白图像;
创建 graphics2d 对象,通过 graphics2d 对象的 drawimage() 方法将待合成图和模板图绘制进空白图像的指定位置(需要注意图层顺序,先绘制待合成图,后绘制模板图);
输出空白图像(合成后的图像)到本地。
代码图片尺寸调整、旋转使用到了 thumbnailator, 需添加 maven 依赖
<dependency> <groupid>net.coobird</groupid> <artifactid>thumbnailator</artifactid> <version>0.4.8</version></dependency>
/** * 横坐标 */private static final int x = 457;/** * 纵坐标 */private static final int y = 295;/** * 旋转角度 */private static final double angle = 16;/** * 缩放比例 */private static final double scale = 0.18;/** * 图片合成 * * @param imagepath 待合成的图片路径 * @param outpath 合成后的图片输出路径 * @throws ioexception */private static void synthesis(string imagepath, string outpath) throws ioexception { // 模板图 bufferedimage template = imageio.read(new file("d:\\local\\images\\template.png")); // 待合成图 bufferedimage image = imageio.read(new file(imagepath)); // 调整待合成图的尺寸和旋转角度 image = thumbnails.of(image).scale(scale).rotate(angle).asbufferedimage(); // 合成后的图 bufferedimage result = new bufferedimage(template.getwidth(), template.getheight(), template.gettype()); graphics2d graphics2d = result.creategraphics(); // 先画待合成图,后画模板图,这样就能将待合成图放置在模板图的下层 graphics2d.drawimage(image, x, y, null); graphics2d.drawimage(template,0,0, null); graphics2d.dispose(); imageio.write(result, "png", new file(outpath));}
运行代码public static void main(string[] args) throws ioexception { synthesis("d:\\local\\images\\weixin_payment_code.png", "d:\\local\\images\\result.png");}
调整图片亮度、对比度/** * 调整亮度、对比度 * * @param image */private static void adjustbrightnessandcontrast(bufferedimage image) { int width = image.getwidth(); int height = image.getheight(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { color color = new color(image.getrgb(x, y)); int red = calculatecolor(color.getred()); int green = calculatecolor(color.getgreen()); int blue = calculatecolor(color.getblue()); color = new color(red, green, blue); image.setrgb(x, y, color.getrgb()); } }}/** * 亮度,取值范围[-1,1] */private static final double brightness = 0;/** * 对比度,取值范围[-1,1] */private static final double contrast = -0.5;/** * 计算亮度、对比度颜色值 * * @param color 原颜色值 * @return 返回计算后的颜色值 */private static int calculatecolor(int color) { color = (int)((color - 127.5 * (1 - brightness)) * math.tan((45 + 44 * contrast) / 180 * math.pi) + 127.5 * (1 + brightness)); return getcolor(color);}/** * 获取范围内的颜色值,[0,255] * * @param color * @return */private static int getcolor(int color) { return color > 255 ? 255 : color < 0 ? 0 : color;}
在处理待合成图片的尺寸和旋转角度后调用 adjustbrightnessandcontrast() 方法调整亮度和对比度
...// 调整待合成图的尺寸和旋转角度image = thumbnails.of(image).scale(scale).rotate(angle).asbufferedimage();// 调整待合成图片的亮度、对比度adjustbrightnessandcontrast(image);...
重新运行代码
以上就是java如何实现图像处理和p图?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product