zoukankan      html  css  js  c++  java
  • java web 打水印

    1. /**
    2. * 把图片印刷到图片上
    3. *
    4. * @param pressImg --
    5. * 水印文件
    6. * @param targetinp --
    7. * 目标文件
    8. * @param x
    9. * --x坐标
    10. * @param y
    11. * --y坐标
    12. */
    13. public static File pressImage(File pressImg, InputStream targetinp,String format,
    14. int x, int y) {
    15. File file = null;
    16. try {
    17. //目标文件
    18. Image src = ImageIO.read(targetinp);
    19. int wideth = src.getWidth(null);
    20. int height = src.getHeight(null);
    21. BufferedImage image = new BufferedImage(wideth, height,
    22. BufferedImage.TYPE_INT_RGB);
    23. Graphics g = image.createGraphics();
    24. g.drawImage(src, 0, 0, wideth, height, null);
    25. //水印文件
    26. Image src_biao = ImageIO.read(pressImg);
    27. int wideth_biao = src_biao.getWidth(null);
    28. int height_biao = src_biao.getHeight(null);
    29. g.drawImage(src_biao, (wideth - wideth_biao),
    30. (height - height_biao)-40 , wideth_biao, height_biao, null);
    31. //水印文件结束
    32. g.dispose();
    33. ByteArrayOutputStream os = new ByteArrayOutputStream();
    34. ImageIO.write(image,format,os);
    35. byte [] bytes = os.toByteArray();
    36. BufferedOutputStream bos = null;
    37. FileOutputStream fos = null;
    38. file = new File("temp");
    39. fos = new FileOutputStream(file);
    40. bos = new BufferedOutputStream(fos);
    41. bos.write(bytes);
    42. bos.close();
    43. fos.close();
    44. os.close();
    45. } catch (Exception e) {
    46. e.printStackTrace();
    47. }
    48. return file;
    49. }

    以上工具代码

    无论压缩 水印   都是差不多用Image.read  
    在spring muitpartfile 读取传入的file ,出现 can't read input file , 由于传入file 引起的问题
    改为 inputstream即可
    不过最好使用InputStream读


    ps :


    ImageIO.read(targetinp);

    ImageIO的read 静态方法 可以传入 url , inputstream , file 多个重载方法




  • 相关阅读:
    眼睛的颜色 博弈
    codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数
    10 25日考试 数学题目练习 斐波拉契 打表
    线段树 模板
    榨取kkksc03 luogu1855 dp 裸二维费用背包
    低价购买 洛谷1108 codevs4748 dp
    [转] 经典排序算法
    [USACO08DEC] Trick or Treat on the Farm
    [NOIP2009] 靶形数独(搜索+剪枝)
    各种蒟蒻模板【如此简单】
  • 原文地址:https://www.cnblogs.com/signheart/p/4aa62969a10b73218eaaa2c3cc1f730e.html
Copyright © 2011-2022 走看看