JPEG 이미지의 크기를 조정하거나 축소판을 만들 때 Java AWT를 사용하고 있습니다. 이미지가 정상 샘플링 팩터 (2x2,1x1,1x1)를 가지고있을 때 코드가 잘 작동합니다.Java에서 비표준 샘플링 요소로 JPEG 이미지를 스케일하는 방법은 무엇입니까?
그러나이 샘플링 요소 (1x1, 1x1, 1x1)가있는 이미지는 크기가 조정될 때 문제를 만듭니다. 기능은 인식 가능하지만 색상이 손상됩니다.
original과 썸네일 : alt text http://otherplace.in/thumb1.jpg
내가 사용하고 코드에 거의 동일합니다 :
static BufferedImage awtScaleImage(BufferedImage image,
int maxSize, int hint) {
// We use AWT Image scaling because it has far superior quality
// compared to JAI scaling. It also performs better (speed)!
System.out.println("AWT Scaling image to: " + maxSize);
int w = image.getWidth();
int h = image.getHeight();
float scaleFactor = 1.0f;
if (w > h)
scaleFactor = ((float) maxSize/(float) w);
else
scaleFactor = ((float) maxSize/(float) h);
w = (int)(w * scaleFactor);
h = (int)(h * scaleFactor);
// since this code can run both headless and in a graphics context
// we will just create a standard rgb image here and take the
// performance hit in a non-compatible image format if any
Image i = image.getScaledInstance(w, h, hint);
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.drawImage(i, null, null);
g.dispose();
i.flush();
return image;
}
(this page의 코드 제공)
할 수있는 더 좋은 방법이 있나요 이?
샘플링 비율이 [1x1, 1x1, 1x1] 인 test image입니다.
'ImageIO'를 사용하여 * 반투명 * 이미지를 JPEG로 인코딩 할 때이 효과를 보았습니다. 그러나 출력 이미지가 불투명 (TYPE_INT_RGB')하여이 예제가 적용되지 않는다고 생각합니다.이 코드는 샘플이 완료되었거나 이미지에 다른 후 처리가 적용되고 있습니까? 의도하지 않게 반투명 이미지를 만들 수 있습니다 (예 :'AffineTransformOp' with'TYPE_BILINEAR'는 정확한 픽셀 경계에 있지 않을 경우 결과 이미지의 가장자리에 앤티 앨리어싱을 추가합니다.) – finnw