【发布时间】:2011-08-19 05:52:16
【问题描述】:
我有一个 PNG 图像,我想调整它的大小。我怎样才能做到这一点?虽然我已经通过this 看不懂sn-p。
【问题讨论】:
-
你对那个sn-p到底有什么不明白的?
-
您想生成原始 png 的调整大小版本,还是只是在 UI 中的某个位置绘制调整大小的版本?
标签: java swing user-interface image
我有一个 PNG 图像,我想调整它的大小。我怎样才能做到这一点?虽然我已经通过this 看不懂sn-p。
【问题讨论】:
标签: java swing user-interface image
如果您有 java.awt.Image,调整其大小不需要任何额外的库。做吧:
Image newImage = yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
显然,替换 newWidth 和 newHeight 为指定图像的尺寸。
注意最后一个参数:它告诉运行时您要用于调整大小的算法。
有些算法可以产生非常精确的结果,但是这些算法需要很长时间才能完成。
您可以使用以下任何一种算法:
Image.SCALE_DEFAULT:使用默认的图片缩放算法。Image.SCALE_FAST:选择缩放速度优先于缩放图像平滑度的图像缩放算法。Image.SCALE_SMOOTH:选择图像平滑度比缩放速度优先级更高的图像缩放算法。Image.SCALE_AREA_AVERAGING:使用面积平均图像缩放算法。Image.SCALE_REPLICATE:使用ReplicateScaleFilter类中体现的图像缩放算法。请参阅Javadoc 了解更多信息。
【讨论】:
我们这样做是为了创建图像的缩略图:
BufferedImage tThumbImage = new BufferedImage( tThumbWidth, tThumbHeight, BufferedImage.TYPE_INT_RGB );
Graphics2D tGraphics2D = tThumbImage.createGraphics(); //create a graphics object to paint to
tGraphics2D.setBackground( Color.WHITE );
tGraphics2D.setPaint( Color.WHITE );
tGraphics2D.fillRect( 0, 0, tThumbWidth, tThumbHeight );
tGraphics2D.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
tGraphics2D.drawImage( tOriginalImage, 0, 0, tThumbWidth, tThumbHeight, null ); //draw the image scaled
ImageIO.write( tThumbImage, "JPG", tThumbnailTarget ); //write the image to a file
【讨论】:
试试这个:
ImageIcon icon = new ImageIcon(UrlToPngFile);
Image scaleImage = icon.getImage().getScaledInstance(28, 28,Image.SCALE_DEFAULT);
【讨论】:
ImageIcon,直接ImageIO.read(...)即可。
以高质量调整图像大小:
private static InputStream resizeImage(InputStream uploadedInputStream, String fileName, int width, int height) {
try {
BufferedImage image = ImageIO.read(uploadedInputStream);
Image originalImage= image.getScaledInstance(width, height, Image.SCALE_DEFAULT);
int type = ((image.getType() == 0) ? BufferedImage.TYPE_INT_ARGB : image.getType());
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g2d = resizedImage.createGraphics();
g2d.drawImage(originalImage, 0, 0, width, height, null);
g2d.dispose();
g2d.setComposite(AlphaComposite.Src);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(resizedImage, fileName.split("\\.")[1], byteArrayOutputStream);
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
} catch (IOException e) {
// Something is going wrong while resizing image
return uploadedInputStream;
}
}
【讨论】:
int newHeight = 150;
int newWidth = 150;
holder.iv_arrow.requestLayout();
holder.iv_arrow.getLayoutParams().height = newHeight;
holder.iv_arrow.getLayoutParams().width = newWidth;
holder.iv_arrow.setScaleType(ImageView.ScaleType.FIT_XY);
holder.iv_arrow.setImageResource(R.drawable.video_menu);
【讨论】:
Java 中的简单方法
public void resize(String inputImagePath,
String outputImagePath, int scaledWidth, int scaledHeight)
throws IOException {
// reads input image
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
// creates output image
BufferedImage outputImage = new BufferedImage(scaledWidth,
scaledHeight, inputImage.getType());
// scales the input image to the output image
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();
// extracts extension of output file
String formatName = outputImagePath.substring(outputImagePath
.lastIndexOf(".") + 1);
// writes to output file
ImageIO.write(outputImage, formatName, new File(outputImagePath));
}
【讨论】: