java 图片打水印时,遇到部分图片打完水印后颜色失真,求高手指点 5
源码如下:/***图片打水印*@parambgImage背景图*@paramwaterImg水印图*@paramuniqueFlag生成的新图片名称中的唯一标识,用来保证...
源码如下:
/** * 图片打水印 * @param bgImage 背景图 * @param waterImg 水印图 * @param uniqueFlag 生成的新图片名称中的唯一标识,用来保证生成的图片名称不重复,如果为空或为null,将使用当前时间作为标识 * @return 新图片路径 */ public static String addImageWater(String bgImage, String waterImg ,String uniqueFlag) {
int x = 0; int y = 0; String newImgPath = ""; if(null == uniqueFlag){ uniqueFlag = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); }else if(uniqueFlag.trim().length() < 1){ uniqueFlag = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); } try { File file = new File(bgImage); String fileName = file.getName(); Image image = ImageIO.read(file); int width = image.getWidth(null); int height = image.getHeight(null); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bufferedImage.createGraphics(); g.drawImage(image, 0, 0, width, height, null);
Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件 int width_water = waterImage.getWidth(null); int height_water = waterImage.getHeight(null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
int widthDiff = width - width_water; int heightDiff = height - height_water;
x = widthDiff / 2; y = heightDiff / 2; g.drawImage(waterImage, x, y, width_water, height_water, null); // 水印文件结束 g.dispose(); if(bgImage.contains(fileName)){ newImgPath = bgImage.replace(fileName, uniqueFlag+fileName); } File newImg = new File(newImgPath); ImageIO.write(bufferedImage, "png", newImg); File waterFile = new File(waterImg); if(file.exists()){ file.delete(); } if(waterFile.exists()){ waterFile.delete(); } } catch (IOException e) { e.printStackTrace(); } return newImgPath; }
底图:
水印:
打完水印:
发现只要是从淘宝网站上右键另存的图片都存在这个现象,其他图片没发现异常,求解。。。。咋处理,求高手指点吖。。。。 展开
/** * 图片打水印 * @param bgImage 背景图 * @param waterImg 水印图 * @param uniqueFlag 生成的新图片名称中的唯一标识,用来保证生成的图片名称不重复,如果为空或为null,将使用当前时间作为标识 * @return 新图片路径 */ public static String addImageWater(String bgImage, String waterImg ,String uniqueFlag) {
int x = 0; int y = 0; String newImgPath = ""; if(null == uniqueFlag){ uniqueFlag = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); }else if(uniqueFlag.trim().length() < 1){ uniqueFlag = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); } try { File file = new File(bgImage); String fileName = file.getName(); Image image = ImageIO.read(file); int width = image.getWidth(null); int height = image.getHeight(null); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = bufferedImage.createGraphics(); g.drawImage(image, 0, 0, width, height, null);
Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件 int width_water = waterImage.getWidth(null); int height_water = waterImage.getHeight(null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
int widthDiff = width - width_water; int heightDiff = height - height_water;
x = widthDiff / 2; y = heightDiff / 2; g.drawImage(waterImage, x, y, width_water, height_water, null); // 水印文件结束 g.dispose(); if(bgImage.contains(fileName)){ newImgPath = bgImage.replace(fileName, uniqueFlag+fileName); } File newImg = new File(newImgPath); ImageIO.write(bufferedImage, "png", newImg); File waterFile = new File(waterImg); if(file.exists()){ file.delete(); } if(waterFile.exists()){ waterFile.delete(); } } catch (IOException e) { e.printStackTrace(); } return newImgPath; }
底图:
水印:
打完水印:
发现只要是从淘宝网站上右键另存的图片都存在这个现象,其他图片没发现异常,求解。。。。咋处理,求高手指点吖。。。。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |