JAVA (Graphics2D)解决合成图片失真问题

附对比图:

合成后失真严重

代码优化后:

原代码:

参数解释:

1. img为你需要把他合成到A图片上的B图片,以下简称为章

2. srcImagePath为A图片的绝对路径

3. targetPath为合成后的图片输出路径

4. x,y为章需要合成到A图片上的位置坐标,w,h为将章缩小到多少长宽比例

注意这行代码:g.drawImage(img, x, y, w, h, null);

public static void markByIcon(Image img, String srcImagePath, String targetPath, int x, int y,
      int w, int h) throws IOException {
    OutputStream os = null;
    try {
      logger.info("图片水印开始添加。。。");
      logger.info("图片水印输入路径" + srcImagePath);
      logger.info("图片水印输出路径" + targetPath);
      Image srcImage = ImageIO.read(new File(srcImagePath));
      BufferedImage buffImg = new BufferedImage(srcImage.getWidth(null), srcImage.getHeight(null),
          BufferedImage.TYPE_INT_BGR);
      Graphics2D g = buffImg.createGraphics();
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
          RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g.drawImage(srcImage
              .getScaledInstance(srcImage.getWidth(null), srcImage.getHeight(null), Image.SCALE_SMOOTH),
          0, 0, null);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
      g.drawImage(img, x, y, w, h, null);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
      g.dispose();
      os = new FileOutputStream(targetPath);
      //生成图片
      ImageIO.write(buffImg, "JPG", os);
      logger.info("图片水印添加成功。。。");
    } catch (Exception e) {
      logger.error("图片水印添加失败。。。", e);
      throw e;
    } finally {
      try {
        if (null != os) {
          os.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

优化后代码:

原代码是将章在合成的时候按比例缩小,在缩小过程中失真了,优化后为先将章缩小到想要的比例,然后用缩小后的章进行合成

压缩代码:

/**
   * * 将图片按照指定的图片尺寸压缩 * * @param srcImgPath :源图片路径 * @param outImgPath * :输出的压缩图片的路径 * @param new_w
   * * :压缩后的图片宽 * @param new_h * :压缩后的图片高
   */
  public static BufferedImage compressImage(String srcImgPath, int new_w, int new_h) {
    BufferedImage src = inputImage(srcImgPath);
    BufferedImage bufferedImage = disposeImage(src, new_w, new_h);
    return bufferedImage;
  }

  /**
   * 处理图片 * * @param src * @param outImgPath * @param new_w * @param new_h
   */
  private synchronized static BufferedImage disposeImage(BufferedImage src, int new_w, int new_h) {
    // 得到图片
    int old_w = src.getWidth();
    // 得到源图宽
    int old_h = src.getHeight();
    // 得到源图长
    BufferedImage newImg = null;
    // 判断输入图片的类型
    switch (src.getType()) {
      case 13:
        // png,gifnewImg = new BufferedImage(new_w, new_h,
        // BufferedImage.TYPE_4BYTE_ABGR);
        break;
      default:
        newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
        break;
    }
    Graphics2D g = newImg.createGraphics();
    // 从原图上取颜色绘制新图
    g.drawImage(src, 0, 0, old_w, old_h, null);
    g.dispose();
    // 根据图片尺寸压缩比得到新图的尺寸
    newImg.getGraphics().drawImage(
            src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0,
            null);
    // 调用方法输出图片文件
    return newImg;
  }

  /**
   * * 将图片文件输出到指定的路径,并可设定压缩质量 * * @param outImgPath * @param newImg * @param per
   */
  private static void OutImage(String outImgPath, BufferedImage newImg) {
    // 判断输出的文件夹路径是否存在,不存在则创建
    File file = new File(outImgPath);
    if (!file.getParentFile().exists()) {
      file.getParentFile().mkdirs();
    }// 输出到文件流
    try {
      ImageIO.write(newImg, outImgPath.substring(outImgPath
              .lastIndexOf(".") + 1), new File(outImgPath));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

优化后代码:

先通过compressImage方法将章图片进行比例缩小,然后用缩小后的bufferedImage去进行合成

BufferedImage bufferedImage = ImageUtil.compressImage(signPath, photoConfig.getWidth(), photoConfig.getHeight());
Image signImg = ImageUtil.sign(bufferedImage);
if (signImg == null) {
   throw new MyException("图片生成失败,签名异常");
}
ImageUtil.markBySignIcon(signImg, imagePath, targetPath, x, y, photoConfig.getWidth(), photoConfig.getHeight());

此时我将原markByIcon方法进行了修改,修改后代码如下,修改后的方法名为markBySignIcon如上最后一行

public static void markBySignIcon(Image img, String srcImagePath, String targetPath, int x, int y,
                                    int w, int h) throws IOException {
    OutputStream os = null;
    try {
      logger.info("图片水印开始添加。。。");
      logger.info("图片水印输入路径" + srcImagePath);
      logger.info("图片水印输出路径" + targetPath);
      Image srcImage = ImageIO.read(new File(srcImagePath));
      BufferedImage buffImg = new BufferedImage(srcImage.getWidth(null), srcImage.getHeight(null),
              BufferedImage.TYPE_INT_BGR);
      Graphics2D g = buffImg.createGraphics();
      g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
              RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g.drawImage(srcImage
                      .getScaledInstance(srcImage.getWidth(null), srcImage.getHeight(null), Image.SCALE_SMOOTH),
              0, 0, null);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
      g.drawImage(img, x, y, img.getWidth(null), img.getHeight(null), null);
      g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
      g.dispose();
      os = new FileOutputStream(targetPath);
      //生成图片
      ImageIO.write(buffImg, "JPG", os);
      logger.info("图片水印添加成功。。。");
    } catch (Exception e) {
      logger.error("图片水印添加失败。。。", e);
      throw e;
    } finally {
      try {
        if (null != os) {
          os.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

还记得之前标红,要注意的代码么,现在改成如下

g.drawImage(img, x, y, img.getWidth(null), img.getHeight(null), null);

到此,用来合成的小图章在合成后,效果会较原来好很多

要在Java中模糊背景,可以使用Java 2D API中的`Graphics2D`类。具体步骤如下: 1. 创建一个`BufferedImage`对象,将要绘制的背景图像绘制到该对象上。 2. 使用`java.awt.image.Kernel`类创建一个模糊卷积核,可以通过调整卷积核的大小和值来控制模糊程度。 3. 使用`java.awt.image.ConvolveOp`类创建一个卷积操作对象,并将卷积核作为参数传入。 4. 使用卷积操作对象对`BufferedImage`对象进行滤波操作,得到模糊后的图像。 5. 将模糊后的图像绘制到目标组件的`Graphics`对象上。 下面是一个简单的示例代码,可以将指定的背景图像模糊并绘制到`JPanel`组件上: ```java import java.awt.*; import java.awt.image.*; import javax.swing.*; public class BlurredBackgroundPanel extends JPanel { private BufferedImage background; public BlurredBackgroundPanel(Image background) { this.background = toBufferedImage(background); } private static BufferedImage toBufferedImage(Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bimage.createGraphics(); g2d.drawImage(img, 0, 0, null); g2d.dispose(); return bimage; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); // 创建模糊卷积核 float[] blurKernel = { 1/9f, 1/9f, 1/9f, 1/9f, 1/9f, 1/9f, 1/9f, 1/9f, 1/9f }; Kernel kernel = new Kernel(3, 3, blurKernel); // 创建卷积操作对象 ConvolveOp op = new ConvolveOp(kernel); // 对背景图像进行滤波操作 BufferedImage blurred = op.filter(background, null); // 绘制模糊后的背景图像 g2d.drawImage(blurred, 0, 0, getWidth(), getHeight(), null); g2d.dispose(); } public static void main(String[] args) { JFrame frame = new JFrame("Blurred Background Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); // 加载背景图像 Image background = new ImageIcon("background.jpg").getImage(); // 创建模糊背景面板 BlurredBackgroundPanel panel = new BlurredBackgroundPanel(background); frame.setContentPane(panel); frame.setVisible(true); } } ``` 在上面的示例代码中,我们创建了一个名为`BlurredBackgroundPanel`的自定义`JPanel`组件,它可以将指定的背景图像模糊并绘制到自身上。在`paintComponent`方法中,我们创建了一个`Graphics2D`对象,并通过模糊卷积核和卷积操作对象对背景图像进行了滤波操作,得到了模糊后的图像。最后,我们将模糊后的图像绘制到了目标组件的`Graphics`对象上。 需要注意的是,模糊操作是比较耗时的,如果需要实时模糊显示,可能需要使用多线程或GPU加速等技术来提高性能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值