이미지 크기 조정에 문제가 있습니다. 다음 코드를 사용하여 이미지의 크기를 조절할 때 이미지의 아래쪽이나 오른쪽에 줄이 생깁니다.Java : AffineTransform을 사용하여 이미지 크기를 조절할 때 줄이 나타납니다.
double scale = 1;
if (scaleHeight >= scaleWidth) {
scale = scaleWidth;
} else {
scale = scaleHeight;
}
AffineTransform af = new AffineTransform();
af.scale(scale, scale);
AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage bufferedThumb = operation.filter(img, null);
원본 이미지
선이 나타나는 이유
사람이 알고 있나요 스케일링 된 이미지?
감사합니다.
편집 :
추가 된 전체 메소드 코드 :
public static final int SPINNER_MAX_WIDTH = 105;
public static final int SPINNER_MAX_HEIGHT = 70;
public void scaleImage(BufferedImage img, int maxWidth, int maxHeight, String fileName) {
double scaleWidth = 1;
double scaleHeight = 1;
if (maxHeight != NOT_SET) {
if (img.getHeight() > maxHeight) {
scaleHeight = (double) maxHeight/(double) img.getHeight();
}
}
if (maxWidth != NOT_SET) {
if (img.getWidth() > maxWidth) {
scaleWidth = (double) maxWidth/(double) img.getWidth();
}
}
double scale = 1;
if (scaleHeight >= scaleWidth) {
scale = scaleWidth;
} else {
scale = scaleHeight;
}
AffineTransform af = new AffineTransform();
af.scale(scale, scale);
AffineTransformOp operation = new AffineTransformOp(af, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage bufferedThumb = operation.filter(img, null);
if (bufferedThumb != null) {
File imageFile = new File(fileName);
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
try {
ImageIO.write(bufferedThumb, fileType, imageFile);
} catch (IOException e) {
logger.error("Failed to save scaled image: " + fileName + "\n" + e.getMessage());
}
}
}
SPINNER_MAX_ * 상수로 설정되어 메소드 호출에의 maxWidth 및 maxHeight의 매개 변수를 설정합니다.
감사합니다.
이미지를 입력으로 사용하여 코드를 실행했는데 문제가 없습니다. 'scaleWidth' /'scaleHeight'에 무엇을 사용하고 있습니까? 어떻게 이미지를로드하고 있습니까? 어떻게 표시하고 저장합니까? – Ash
나는 완벽한 방법을 추가했다. - 고마워. – Malakim