Java生成图片红色问题

Java上传图片时,对某些图片进行缩放、裁剪或者生成缩略图时会蒙上一层红色, 经过检查只要经过ImageIO.read()方法读取后再保存,该图片便已经变成红图。 因此,可以推测直接原因在于ImageIO.read()方法加载图片的过程存在问题。

1、解决 :而使用JDK中提供的Image

如果是file
Image src=Toolkit.getDefaultToolkit().getImage(file.getPath());

如果是url
URL url = new URL(wechat_erweimaTmail);
java.awt.Image imageTookittitle = Toolkit.getDefaultToolkit().createImage(url);
BufferedImage titleLab = ImageUtils.toBufferedImage(imageTookittitle);



public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }
    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    try {
        int transparency = Transparency.OPAQUE;
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null),
                image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }
    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        bimage = new BufferedImage(image.getWidth(null),
                image.getHeight(null), type);
    }
    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}



ContactAuthor

要在Java生成图片并添加浮印,可以使用Java的Graphics2D类。Graphics2D类是Java 2D API的一部分,提供了许多绘图方法,可以用来在图像上绘制文本、线条、形状等。 下面是一个简单的Java代码示例,可以生成一个红色背景图片,并在图片上添加一个黑色的浮印: ``` import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageWithWatermark { public static void main(String[] args) throws IOException { int width = 500; int height = 500; BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Fill the background with red color Graphics2D g2 = img.createGraphics(); g2.setColor(Color.RED); g2.fillRect(0, 0, width, height); // Add watermark text g2.setColor(Color.BLACK); g2.setFont(new Font("Arial", Font.BOLD, 30)); String watermark = "Sample Watermark"; int stringWidth = g2.getFontMetrics().stringWidth(watermark); int x = (width - stringWidth) / 2; int y = height / 2; g2.drawString(watermark, x, y); // Save the image with watermark File output = new File("image-with-watermark.png"); ImageIO.write(img, "png", output); } } ``` 在这个示例中,我们首先创建一个BufferedImage对象,并使用Graphics2D类来绘制一个红色背景。然后,我们使用Graphics2D类的setFont()方法设置文本字体和大小,使用drawString()方法将水印文本绘制到图像中心。最后,我们使用ImageIO类将生成的图像保存到文件中。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值