具体代码:
// 水印透明度 private static float alpha = 0.5f; /** * 给图片添加水印图片、可设置水印图片旋转角度 * * @param iconpath 水印图片路径 * @param srcimgpath 源图片路径 * @param location 水印图片位置 * @param degree 水印图片旋转角度 */ public static void markimagebyicon(httpservletrequest request, httpservletresponse response, string iconpath, string srcimgpath, string location, integer degree) { outputstream os = null; try { image srcimg = imageio.read(new file(srcimgpath)); bufferedimage buffimg = new bufferedimage(srcimg.getwidth(null), srcimg.getheight(null), bufferedimage.type_int_rgb); // 得到画笔对象 graphics2d g = buffimg.creategraphics(); // 设置对线段的锯齿状边缘处理 g.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); g.drawimage( srcimg.getscaledinstance(srcimg.getwidth(null), srcimg.getheight(null), image.scale_smooth), 0, 0, null); // 设置水印旋转角度(默认对角线角度) if (null != degree) { g.rotate(math.toradians(degree), (double) buffimg.getwidth() / 2, (double) buffimg.getheight() / 2); } else { //根据三角形相关定理,计算对角线角度 double lengthofdiagonal = math.sqrt(buffimg.getwidth() * buffimg.getwidth() buffimg.getheight() * buffimg.getheight()); double v = (math.pow(buffimg.getwidth(), 2) math.pow(lengthofdiagonal, 2) - math.pow(buffimg.getheight(), 2)) / (2 * buffimg.getwidth() * lengthofdiagonal); double acos = math.acos(v); double mydegree = math.todegrees(acos); g.rotate(-math.toradians(mydegree), (double) buffimg.getwidth() / 2, (double) buffimg.getheight() / 2); } // 水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度 imageicon imgicon = new imageicon(iconpath); image img = imgicon.getimage(); g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, alpha)); // 水印图片的位置 int x = 0, y = 0; if (stringutils.equals(location, "left-top")) { x = 30; y = 30; } else if (stringutils.equals(location, "right-top")) { x = buffimg.getwidth() - img.getwidth(null) - 30; y = 30; } else if (stringutils.equals(location, "left-bottom")) { x = 30; y = buffimg.getheight() - img.getheight(null) - 30; } else if (stringutils.equals(location, "right-bottom")) { x = buffimg.getwidth() - img.getwidth(null) - 30; y = buffimg.getheight() - img.getheight(null) - 30; } else { x = (buffimg.getwidth() - img.getwidth(null)) / 2; y = (buffimg.getheight() - img.getheight(null)) / 2; } g.drawimage(img, x, y, null); g.setcomposite(alphacomposite.getinstance(alphacomposite.src_over)); g.dispose(); // os = new fileoutputstream(targerpath); os = response.getoutputstream(); imageio.write(buffimg, "jpg", os); } catch (exception e) { e.printstacktrace(); } finally { try { if (null != os) os.close(); } catch (exception e) { e.printstacktrace(); } } }
smbfile时也可以写成这样:
// 展示时 添加水印 public void showremarkimage(string filepath, string iconpath, httpservletrequest request, httpservletresponse response) { inputstream is = null; outputstream os = null; bytearrayoutputstream out = new bytearrayoutputstream(); try { smbfile smbfile = new smbfile(filepath); is = smbfile.getinputstream(); os = response.getoutputstream(); image srcimg = imageio.read(is); bufferedimage buffimg = new bufferedimage(srcimg.getwidth(null), srcimg.getheight(null), bufferedimage.type_int_rgb); // 得到画笔对象 graphics2d g = buffimg.creategraphics(); // 设置对线段的锯齿状边缘处理 g.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); g.drawimage(srcimg.getscaledinstance(srcimg.getwidth(null), srcimg.getheight(null), image.scale_smooth), 0, 0, null); // 旋转角度处理(根据三角形相关定理,计算对角线角度) // double lengthofdiagonal = math.sqrt(buffimg.getwidth() * buffimg.getwidth() buffimg.getheight() * buffimg.getheight()); // double v = (math.pow(buffimg.getwidth(), 2) math.pow(lengthofdiagonal, 2) - math.pow(buffimg.getheight(), 2)) / (2 * buffimg.getwidth() * lengthofdiagonal); // double acos = math.acos(v); // double mydegree = math.todegrees(acos); // g.rotate(-math.toradians(mydegree), (double) buffimg.getwidth() / 2, (double) buffimg.getheight() / 2); // 水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度 imageicon imgicon = new imageicon(iconpath); image img = imgicon.getimage(); g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, alpha)); // 水印图片居中 int x = 0, y = 0; x = (buffimg.getwidth() - img.getwidth(null)) / 2; y = (buffimg.getheight() - img.getheight(null)) / 2; g.drawimage(img, x, y, null); g.setcomposite(alphacomposite.getinstance(alphacomposite.src_over)); g.dispose(); imageio.write(buffimg, "png", out); byte[] b = out.tobytearray(); response.addheader("content-length", string.valueof(b.length)); os.write(b, 0, b.length); os.flush(); } catch (exception e) { logutil.error("附件下载失败,时间:" dateutil.formattodatetimetext(new date()) "原因:" e.getmessage()); // throw new applicationexception("文件读取失败:" e.getmessage(), e); } finally { ioutils.closequietly(is); ioutils.closequietly(os); ioutils.closequietly(out); } }