亚洲女人被黑人巨大进入-亚洲日本视频在线观看-亚洲AV秘片一区二区三3-亚洲欧美中文字幕乱码在线

<dfn id="uqq4w"><dl id="uqq4w"></dl></dfn>
  • <abbr id="uqq4w"></abbr>
    <center id="uqq4w"><cite id="uqq4w"></cite></center>
    ?
    徐州北大青鳥(niǎo)

    利用Java加圖片水印以及文字水印方法

    時(shí)間:2016-10-12 11:14來(lái)源:中博IT教育 作者:中博IT教育 點(diǎn)擊:
    今天有個(gè)學(xué)生說(shuō),他想把他上傳的圖片加上個(gè)水印的功能,以防止別人盜用他的圖片。他認(rèn)為他的圖片資料很重要。所以。。。 好,有需求,我們就滿(mǎn)足他,以前我也比較少寫(xiě)操作圖片

    今天有個(gè)學(xué)生說(shuō),他想把他上傳的圖片加上個(gè)水印的功能,以防止別人盜用他的圖片。他認(rèn)為他的圖片資料很重要。所以。。。
    好,有需求,我們就滿(mǎn)足他,以前我也比較少寫(xiě)操作圖片的api,所以對(duì)圖片加水印的功能也一直沒(méi)接觸,不過(guò)對(duì)于現(xiàn)在網(wǎng)絡(luò)來(lái)說(shuō)。這些根本就不算什么,上網(wǎng)一搜,就找了幾個(gè)程序出來(lái),現(xiàn)在我重構(gòu)了下,使它滿(mǎn)足我的要求,現(xiàn)在發(fā)布出來(lái),希望可以給有需要的朋友一點(diǎn)幫助。
     

    java 代碼
     
    1. public final class ImageUtils {
    2. public ImageUtils() {
    3.  
    4. }
    5.  
    6. public final static String getPressImgPath(){
    7. return ApplicationContext.getRealPath("/template/data/util/shuiyin.gif");
    8. }
    9.  
    10. /**
    11. * 把圖片印刷到圖片上
    12. * @param pressImg -- 水印文件
    13. * @param targetImg -- 目標(biāo)文件
    14. * @param x
    15. * @param y
    16. */
    17. public final static void pressImage(String pressImg, String targetImg, int x, int y) {
    18. try {
    19. File _file = new File(targetImg);
    20. Image src = ImageIO.read(_file);
    21. int wideth = src.getWidth(null);
    22. int height = src.getHeight(null);
    23. BufferedImage image = new BufferedImage(wideth, height,
    24. BufferedImage.TYPE_INT_RGB);
    25. Graphics g = image.createGraphics();
    26. g.drawImage(src, 0, 0, wideth, height, null);
    27.  
    28. // 水印文件
    29. File _filebiao = new File(pressImg);
    30. Image src_biao = ImageIO.read(_filebiao);
    31. int wideth_biao = src_biao.getWidth(null);
    32. int height_biao = src_biao.getHeight(null);
    33. g.drawImage(src_biao, wideth - wideth_biao - x, height - height_biao -y, wideth_biao,
    34. height_biao, null);
    35. // /
    36. g.dispose();
    37. FileOutputStream out = new FileOutputStream(targetImg);
    38. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    39. encoder.encode(image);
    40. out.close();
    41. } catch (Exception e) {
    42. e.printStackTrace();
    43. }
    44. }
    45.  
    46. /**
    47. * 打印文字水印圖片
    48. * @param pressText --文字
    49. * @param targetImg -- 目標(biāo)圖片
    50. * @param fontName -- 字體名
    51. * @param fontStyle -- 字體樣式
    52. * @param color -- 字體顏色
    53. * @param fontSize -- 字體大小
    54. * @param x -- 偏移量
    55. * @param y
    56. */
    57.  
    58. public static void pressText(String pressText, String targetImg, String fontName,int fontStyle, int color, int fontSize, int x, int y) {
    59. try {
    60. File _file = new File(targetImg);
    61. Image src = ImageIO.read(_file);
    62. int wideth = src.getWidth(null);
    63. int height = src.getHeight(null);
    64. BufferedImage image = new BufferedImage(wideth, height,
    65. BufferedImage.TYPE_INT_RGB);
    66. Graphics g = image.createGraphics();
    67. g.drawImage(src, 0, 0, wideth, height, null);
    68. // String s="www.qhd.com.cn";
    69. g.setColor(Color.RED);
    70. g.setFont(new Font(fontName, fontStyle, fontSize));
    71.  
    72.  
    73. g.drawString(pressText, wideth - fontSize - x, height - fontSize/2 - y);
    74. g.dispose();
    75. FileOutputStream out = new FileOutputStream(targetImg);
    76. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    77. encoder.encode(image);
    78. out.close();
    79. } catch (Exception e) {
    80. System.out.println(e);
    81. }
    82. }
    83.  
    84. public static void main(String[] args) {
    85. pressImage("C:/shuiyin/shuiyin.gif", "c:/shuiyin/DSC02342.JPG", 20 ,20);
    86. }
    87. }
    試聽(tīng)課
    (責(zé)任編輯:代碼如詩(shī))
    ------分隔線(xiàn)----------------------------
    欄目列表
    推薦內(nèi)容