2014-06-18 5 views
0

im4java를 사용하여 입력 스트림의 이미지를 출력 스트림으로 변환하려고합니다. 워드 프로세서, 그것은 입력으로 inputProvider을 설정하고 출력 스트림해야 파이프 변환 된 이미지를 지정처럼 보이는,하지만 난 얻을 :ImageMagick : 파이프 된 이미지 변환

public static InputStream convert(InputStream imageStream){ 
     try { 
      // Set up the im4java properties. See {@link im4java} 
      IMOperation op = new IMOperation(); 
      op.scale(1000); 
      op.compress("Zip"); 
      ConvertCmd convert = new ConvertCmd(); 

      convert.setSearchPath(imLocation); 

      logger.debug("Imagemagick located at: " + convert.getSearchPath()); 

      InputStream is = imageStream; 
      ByteArrayOutputStream os = new ByteArrayOutputStream(); 
      Pipe pipeIn = new Pipe (is, null); 
      Pipe pipeOut = new Pipe(null, os); 

      convert.setInputProvider(pipeIn); 
      convert.setOutputConsumer(pipeOut); 
      convert.run(op, "-", "pdf:-"); 
      is.close(); 
      os.close(); 

      pipeOut.consumeOutput(is); 
      return is; 
     }catch(Exception e){ 
      logger.debug("Failed to convert image: " + e.getMessage()); 
     } 
     return null; 
    } 

감사 :

Failed to convert image: more argument images then placeholders 

가 여기 내 기능입니다!

+0

하지 질문에 대한 대답,하지만 당신은 당신이'is'을 폐쇄하고 그 후'pipeOut.consumeOutput (이다)를 호출 주목 한,'당신은 폐쇄'is'을 반환한다. 그 확인은? 나는 잘 모르겠다. –

답변

0

후손을 위해 여기에 게시했습니다.

public static InputStream convert(String path, InputStream imageStream){ 
    try { 
     IMOperation op = new IMOperation(); 
     op.compress("Zip"); //order matters here, make sure colorspace is the first arg set 

     InputStream is = imageStream; 
     Pipe pipeIn = new Pipe (is, null); 
     ByteArrayOutputStream os = new ByteArrayOutputStream(); 
     Pipe pipeOut = new Pipe(null, os); 
     ConvertCmd convert = new ConvertCmd(); 

     convert.setSearchPath(imLocation); 

     convert.setInputProvider(pipeIn); 
     convert.setOutputConsumer(pipeOut); 

     op.addImage("-"); 
     op.addImage("pdf:-"); 
     // execute the operation 
     final long startTime = System.nanoTime(); 
     convert.run(op); 
     final long endTime = System.nanoTime(); 
     logger.debug("Compressed"); 
     logger.debug("done converting " + path); 
     final long elapsedTimeInMs = (endTime - startTime)/1000000; 
     logger.debug("took " + elapsedTimeInMs); 

     return new ByteArrayInputStream(os.toByteArray()); 
    }catch(Exception e){ 
     logger.debug("Failed to convert image: " + e.getMessage()); 
    } 
    return null; 
}