private void assembleOperation(IMOperation op, Parameters params, Dimension fullSize) {
    // region transformation
    Region region = params.getRegion();
    if (!region.isFull()) {
      if (region.isPercent()) {
        // im4java doesn't support cropping x/y by percentage (only
        // width/height), so we have to calculate them.
        int x = Math.round(region.getX() / 100.0f * fullSize.width);
        int y = Math.round(region.getY() / 100.0f * fullSize.height);
        int width = Math.round(region.getWidth());
        int height = Math.round(region.getHeight());
        op.crop(width, height, x, y, "%");
      } else {
        op.crop(
            Math.round(region.getWidth()), Math.round(region.getHeight()),
            Math.round(region.getX()), Math.round(region.getY()));
      }
    }

    // size transformation
    Size size = params.getSize();
    if (size.getScaleMode() != Size.ScaleMode.FULL) {
      if (size.getScaleMode() == Size.ScaleMode.ASPECT_FIT_WIDTH) {
        op.resize(size.getWidth());
      } else if (size.getScaleMode() == Size.ScaleMode.ASPECT_FIT_HEIGHT) {
        op.resize(null, size.getHeight());
      } else if (size.getScaleMode() == Size.ScaleMode.NON_ASPECT_FILL) {
        op.resize(size.getWidth(), size.getHeight(), "!".charAt(0));
      } else if (size.getScaleMode() == Size.ScaleMode.ASPECT_FIT_INSIDE) {
        op.resize(size.getWidth(), size.getHeight());
      } else if (size.getPercent() != null) {
        op.resize(Math.round(size.getPercent()), Math.round(size.getPercent()), "%".charAt(0));
      }
    }

    // rotation transformation
    Rotation rotation = params.getRotation();
    if (rotation.shouldMirror()) {
      op.flop();
    }
    if (rotation.getDegrees() != 0) {
      op.rotate(rotation.getDegrees().doubleValue());
    }

    // quality transformation
    Quality quality = params.getQuality();
    if (quality != Quality.COLOR && quality != Quality.DEFAULT) {
      switch (quality) {
        case GRAY:
          op.colorspace("Gray");
          break;
        case BITONAL:
          op.monochrome();
          break;
      }
    }
  }
  public static BufferedImage ConvertPageToImage(File afile, int i)
      throws IOException, InterruptedException, IM4JavaException {

    IMOperation op = new IMOperation();
    op.addImage(afile.getAbsolutePath() + "[" + i + "]"); // look at myscript.sh
    op.colorspace("RGB");
    op.resize(640, 830);
    op.type("TrueColor");
    op.addImage("jpg:-"); // place holder for output file

    // op.alpha("off"); //this command is slow and not needed anymore
    //  op.interpolate("spline");
    //  op.colorspace("RGB");
    ConvertCmd convert = new ConvertCmd();
    //		  convert.setSearchPath("/usr/local/Cellar/imagemagick/6.7.7-6/bin/convert");
    //		  convert.setSearchPath("/usr/bin/convert");
    //		  convert.setSearchPath("/Volumes/Main
    // Drive/Users/angellopozo/ImageMagick-6.7.8/bin/convert");
    //		  convert.setSearchPath("Users/angellopozo/ImageMagick-6.7.8/bin/convert");
    //		  convert.setSearchPath("/usr/local/bin");
    Stream2BufferedImage s2b = new Stream2BufferedImage();
    convert.setOutputConsumer(s2b);
    convert.run(op);
    //		  convert.createScript("myscript.sh",op);//debug`` line
    BufferedImage aPDF_img = s2b.getImage(); // PDF_img is the pdf_image
    ImageIO.write(
        aPDF_img,
        "jpeg",
        new File("Page_" + (i + 1) + "of_PDF.jpg")); // write whole pdf page to png
    return aPDF_img;
  } // end of ConvertPagetoImage
Exemple #3
0
 /**
  * 根据尺寸缩放图片
  *
  * @param width 缩放后的图片宽度
  * @param height 缩放后的图片高度
  * @param srcPath 源图片路径
  * @param newPath 缩放后图片的路径
  */
 public static void resizeImage(String srcPath, String newPath, int width, int height)
     throws InterruptedException, IOException, IM4JavaException {
   IMOperation op = new IMOperation();
   op.addImage(srcPath);
   op.resize(width, height);
   op.addImage(newPath);
   // linux下不要设置此值,不然会报错
   if (isWin) {
     convertCmd.setSearchPath(imageMagickPath);
   }
   convertCmd.run(op);
 }
  /**
   * 根据尺寸缩放图片[等比例缩放:参数height为null,按宽度缩放比例缩放;参数width为null,按高度缩放比例缩放]
   *
   * @param imagePath 源图片路径
   * @param newPath 处理后图片路径
   * @param width 缩放后的图片宽度
   * @param height 缩放后的图片高度
   * @return 返回true说明缩放成功,否则失败
   */
  public static boolean zoomImage(
      String imagePath, String newPath, Integer width, Integer height, String quality) {
    boolean flag = false;
    try {
      IMOperation op = new IMOperation();
      op.addImage(imagePath);
      if (width == null) { // 根据高度缩放图片
        op.resize(null, height);
      } else if (height == null) { // 根据宽度缩放图片
        op.resize(width, null);
      } else {
        op.resize(width, height);
      }
      op.addImage(newPath);
      if ((quality != null && quality.equals(""))) {
        op.addRawArgs("-quality", quality);
      }
      ConvertCmd convert = new ConvertCmd(true);
      if (OS_NAME.indexOf("win") >= 0) {
        // linux下不要设置此值,不然会报错
        String path = getGraphicsMagickPath();
        if (StringUtils.isNotBlank(path)) convert.setSearchPath(path);
      }
      convert.run(op);
      flag = true;
    } catch (IOException e) {
      flag = false;
      e.printStackTrace();
    } catch (InterruptedException e) {
      flag = false;
      e.printStackTrace();
    } catch (IM4JavaException e) {
      flag = false;
      e.printStackTrace();
    } finally {

    }
    return flag;
  }