用JMagick实现图片合成

/**
  * 图片合成
  * @param image1 合成图片1
  * @param target 目标图片
  * @param image2 合成图片2
  * @param width  目标图片宽
  * @param height 目标图片高
  * @param xOff X偏移位置
  * @param yOff Y偏移位置
  */
 public void composePhoto(String image1, String target, String image2,
   int width, int height,int xOff,int yOff) throws IOException{
  MagickImage fImage = null;
  MagickImage sImage = null;
  MagickImage fLogo = null;
  MagickImage sLogo = null;
  Dimension logoDim = null;
  try {
   ImageInfo info = new ImageInfo();
   ImageInfo sourceInfo = new ImageInfo(image2);
   fImage = new MagickImage(sourceInfo);
   ImageInfo frameInfo = new ImageInfo(image1);
   fLogo = new MagickImage(frameInfo);
   logoDim = fLogo.getDimension();
   if(width <= 0)
    width = logoDim.width;
   if(height <= 0)
    height = logoDim.height;
   sImage = fImage.scaleImage(width, height);
   sLogo = fLogo.scaleImage(width, height);

//CompositeOperator选择不同的合成操作(本例是用来实现图片相框)
   sImage.compositeImage(CompositeOperator.DstOverCompositeOp, sLogo,xOff,yOff);
   sImage.setFileName(target);
   sImage.writeImage(info);
  }catch (MagickApiException ex) {
   throw new IOException(ex.getMessage());
  } catch (MagickException ex) {
   ex.printStackTrace();
   throw new IOException(ex.getMessage());
  }finally {
   if(sImage != null) 
    sImage.destroyImages();
   if(fImage != null)
    fImage.destroyImages();
   if(fLogo != null)
    fLogo.destroyImages();
   if(sLogo != null)
    sLogo.destroyImages();
  }
 }

你可能感兴趣的:(jmagick)