gpt4 book ai didi

java - 扩展 BufferedImage

转载 作者:行者123 更新时间:2023-11-29 09:45:21 25 4
gpt4 key购买 nike

为什么下面的代码显示的是黑色图像而不是图片?如何正确扩展 BufferedImage?

class SizeOfImage {

public static void main(String[] args) throws Exception {
URL url = new URL("http://cloudbite.co.uk/wp-content/uploads/2011/03/google-chrome-logo-v1.jpg");
final BufferedImage bi = ImageIO.read(url);
final String size = bi.getWidth() + "x" + bi.getHeight();

final CustomImg cstImg = new CustomImg(bi.getWidth(), bi.getHeight(), bi.getType());

SwingUtilities.invokeLater(new Runnable() {

public void run() {
JLabel l = new JLabel(size, new ImageIcon(cstImg), SwingConstants.RIGHT);
JOptionPane.showMessageDialog(null, l);
}
});
}

public static class CustomImg extends BufferedImage {

public CustomImg(int width, int height, int type){
super(width, height, type);
}

}
}

最佳答案

Size of Image

import java.awt.image.BufferedImage;
import java.awt.Graphics;
import javax.swing.*;
import javax.imageio.ImageIO;

import java.net.URL;

class SizeOfImage {

public static void main(String[] args) throws Exception {
URL url = new URL(
"http://cloudbite.co.uk/wp-content/" +
"uploads/2011/03/google-chrome-logo-v1.jpg");
BufferedImage bi = ImageIO.read(url);
final String size = bi.getWidth() + "x" + bi.getHeight();
final CustomImg cstImg = new CustomImg(
bi.getWidth(),
bi.getHeight(), bi.
getType());

// paint something to the new image!
Graphics g = cstImg.createGraphics();
g.drawImage(bi,0,0,null);
g.dispose();

SwingUtilities.invokeLater(new Runnable() {

public void run() {
JLabel l = new JLabel(
size,
new ImageIcon(cstImg),
SwingConstants.RIGHT );
JOptionPane.showMessageDialog(null, l);
}
});
}

public static class CustomImg extends BufferedImage {
public CustomImg(int width, int height, int type){
super(width, height, type);
}
}
}

关于java - 扩展 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8214810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com