2013-02-13 1 views
2

ImageMagick을 사용한 이미지 처리 작업을 위해 im4java 라이브러리를 사용하고 있습니다.im4java를 사용하여 이미지 품질을 향상시키는 방법은 무엇입니까?

코드 내가 좋아하는 외모를 사용하고 있습니다 :

ImageCommand imageCommand = new ConvertCmd(); 
imageCommand.setSearchPath(getImageMagickPath()); 
operation = new IMOperation(); 
operation.quality(100d); 
operation.addImage(); 
operation.resize(getWidth()); 
operation.addImage(); 
imageCommand.run(operation, sourceImage, extention); 

그러나 이미지를 결과의 품질이 좋지 않습니다. 커맨드 라인을 사용하여 똑같은 작업을 수행하면 더 나은 품질로 4 배 더 큰 크기가 제공됩니다. im4java를 사용할 때 추가 설정이 필요합니까? 파일 형식은 jpg입니다. 사전에 감사 ...

답변

0

이것은 내가 사용하고 코드이며, 그것은 마법처럼 작동

private static void saveScaleImage(File image, String outputImagePath, int width, Integer height, float quality, boolean fill) throws Exception { 
    Info info = new Info(image.getPath(), true); 
    String imageFormat = info.getImageFormat(); 
    IMOperation op = new IMOperation(); 
    op.addImage(imagen.getPath()); 
    int imageWidth = info.getImageWidth(), 
     imageHeight = info.getImageHeight(); 
    if (imageWidth> 0 && imageHeight > 0 && (imageFormat == null || !imageFormat.equalsIgnoreCase("TIFF"))) { 
     //fill transparencies && extra space with white 
     op.size(imageWidth, imageHeight); 
     op.addRawArgs("xc:white"); 
     op.addImage(outputImagePath); 
     // set up command 
     CompositeCmd composite = new CompositeCmd(); 
     composite.run(op); 
     op = new IMOperation(); 
     op.addImage(outputImagePath); 
    } 
    //add strip irrelevant info 
    op.strip(); 
    //resize 
    if (fill) { //crop image + fill 
     op.resize(width, height, "^").gravity("center").extent(width, height); 
    } else { //adjust 
     op.resize(width, height); 
    } 
    boolean smaller = imageWidth > 0 && imageHeight > 0 && imageWidth < width && imageHeight < width; 
    if (smaller) { 
     op.addImage(outputImagePath); 
     op.gravity("center"); 
    } 
    if (imageFormat == null || imageFormat.equalsIgnoreCase("PNG")) { 
     //jpeg teawks 
     op.type("optimize").quality((double) quality).blur(0d, .16); 
     //default to jpeg format 
     op.addImage("jpeg:" + outputImagePath); 
    } else { 
     op.addImage(outputImagePath); 
    } 
    // set up command 
    ConvertCmd convert = new ConvertCmd(); 
    //run command 
    convert.run(op); 
}