8
이 코드는 실제로 작동하지만 완벽하지는 않지만 크기가 조정 된 축소판이 흰색으로 그려진 사각형에 붙여지지 않습니다. 이미지 가로 세로 비율을 깨고, 여기에 코드, 누군가가 내게 그것에 대한 수정 제안을 수 있을까요? 당신이 취득하면자바 : 이미지 형식 감지, 크기 조정 (스케일) 및 JPEG로 저장
이BufferedImage img = ImageIO.read(new File("image.jpg"));
int scaleX = (int) (img.getWidth() * 0.5);
int scaleY = (int) (img.getHeight() * 0.5);
Image newImg = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH);
당신의 Image
을 확장 당신이에 그것을 다시 "변환"할 수 있습니다
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ImageScalerImageIoImpl implements ImageScaler {
private static final String OUTPUT_FORMAT_ID = "jpeg";
// Re-scaling image
public byte[] scaleImage(byte[] originalImage, int targetWidth,
int targetHeight) {
try {
InputStream imageStream = new BufferedInputStream(
new ByteArrayInputStream(originalImage));
Image image = (Image) ImageIO.read(imageStream);
int thumbWidth = targetWidth;
int thumbHeight = targetHeight;
// Make sure the aspect ratio is maintained, so the image is not skewed
double thumbRatio = (double)thumbWidth/(double)thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double)imageWidth/(double)imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidth/imageRatio);
} else {
thumbWidth = (int)(thumbHeight * imageRatio);
}
// Draw the scaled image
BufferedImage thumbImage = new BufferedImage(thumbWidth,
thumbHeight, BufferedImage.TYPE_INT_RGB);
System.out.println("Thumb width Buffered: " + thumbWidth + " || Thumb height Buffered: " + thumbHeight);
Graphics2D graphics2D = thumbImage.createGraphics();
// Use of BILNEAR filtering to enable smooth scaling
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
// graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
// White Background
graphics2D.setPaint(Color.WHITE);
graphics2D.fill(new Rectangle2D.Double(0, 0, targetWidth,
targetHeight));
graphics2D.fillRect(0, 0, targetWidth, targetHeight);
System.out.println("Target width: " + targetWidth + " || Target height: " + targetHeight);
// insert the resized thumbnail between X and Y of the image
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
System.out.println("Thumb width: " + thumbWidth + " || Thumb height: " + thumbHeight);
// Write the scaled image to the outputstream
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(thumbImage, OUTPUT_FORMAT_ID, out);
return out.toByteArray();
} catch (IOException ioe) {
throw new ImageResizingException(ioe);
}
}
}
스케일링은 대상 Canvas 컨테이너에 따라 동적이어야합니다. 어쨌든, 필자는 좋은 리펙토링과 몇 가지 논리 수정을 통해 자신을 해결했습니다. 어쨌든 고맙습니다. – BoDiE2003
링크가 작동하지 않습니다. Image to BufferedImage 변환에 대한 힌트 – Karussell
어떤 링크가 작동하지 않습니까? 나는 방금 그들 모두를 시험해 보았다. 그리고 그들은 ok와 같다. – Adamski