示例#1
0
  public RenderedImage convertCMYKtoRGB(byte[] bytes, String type, boolean fork) {

    ImageMagick imageMagick = getImageMagick();

    if (!imageMagick.isEnabled()) {
      return null;
    }

    File inputFile = _fileUtil.createTempFile(type);
    File outputFile = _fileUtil.createTempFile(type);

    try {
      _fileUtil.write(inputFile, bytes);

      IMOperation imOperation = new IMOperation();

      imOperation.addRawArgs("-format", "%[colorspace]");
      imOperation.addImage(inputFile.getPath());

      String[] output = imageMagick.identify(imOperation.getCmdArgs(), fork);

      if ((output.length == 1) && output[0].equalsIgnoreCase("CMYK")) {
        if (_log.isInfoEnabled()) {
          _log.info("The image is in the CMYK colorspace");
        }

        imOperation = new IMOperation();

        imOperation.addRawArgs("-colorspace", "RGB");
        imOperation.addImage(inputFile.getPath());
        imOperation.addImage(outputFile.getPath());

        imageMagick.convert(imOperation.getCmdArgs(), fork);

        bytes = _fileUtil.getBytes(outputFile);

        return read(bytes, type);
      }
    } catch (Exception e) {
      if (_log.isErrorEnabled()) {
        _log.error(e, e);
      }
    } finally {
      _fileUtil.delete(inputFile);
      _fileUtil.delete(outputFile);
    }

    return null;
  }
  /**
   * 根据尺寸缩放图片[等比例缩放:参数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;
  }