gpt4 book ai didi

java - TYPE_INT_ARGB 图像未正确显示

转载 作者:行者123 更新时间:2023-12-02 06:16:40 24 4
gpt4 key购买 nike

我正在创建一个带有随机像素和中间有一个整数的图像。但是,颜色看起来不太对。

click!

正如您所看到的,颜色不正确。这是代码

private void createImage(){
try{
String key = "3534";
BufferedImage thumbnail = new BufferedImage(300, 300,BufferedImage.TYPE_INT_ARGB);
Graphics graphics = thumbnail.getGraphics();
graphics.setFont(new Font(null, Font.BOLD, 100));

randomizePixels(graphics);

graphics.setColor(new Color(255,255,255,255));
graphics.drawString(key, thumbnail.getWidth()/2, thumbnail.getHeight()/2);

ImageIO.write(thumbnail,"jpg",new File("c:\\image1.jpg"));
}catch (Exception e){
e.printStackTrace();
}
}
private void randomizePixels(Graphics graphics){
Random random = new Random();
for(int k=0;k<300;k++){
for(int j=0;j<300;j++){
graphics.setColor(new Color(random.nextFloat(), random.nextFloat(), random.nextFloat(), random.nextFloat()));
graphics.fillRect(k,j,1,1);
}
}
}

如您所见,我设置了数字的颜色 (255,255,255,255),它是白色,不透明度为 100%。

我一定做错了什么吗?如何让我的号码变成白色?

最佳答案

首先,运行您的代码时我得到的结果略有不同。这些点应该具有随机颜色,而不是您发布的全红色。

enter image description here

我不确定你到底想要完成什么,但我猜你想要一个随机噪音并放置一个带有部分可见文本的覆盖层。我首先使用 png 作为输出格式。我不确定 jpg 如何处理透明度。因此,您应该首先用纯色绘制随机像素,然后在其上写入文本。在随机化像素后,您需要移动设置颜色,如下所示:

private static void createImage(){
try{
String key = "3534";
BufferedImage thumbnail = new BufferedImage(300, 300,BufferedImage.TYPE_INT_ARGB);
Graphics graphics = thumbnail.getGraphics();

randomizePixels(graphics);

graphics.setColor(new Color(255, 255, 255 ,255));
graphics.setFont(new Font(null, Font.BOLD, 100));
graphics.drawString(key, thumbnail.getWidth()/2, thumbnail.getHeight()/2);
ImageIO.write(thumbnail, "png", new File("image1.png"));
}catch (Exception e){
e.printStackTrace();
}
}
private static void randomizePixels(Graphics graphics){
Random random = new Random();
for(int k=0;k<300;k++){
for(int j=0;j<300;j++){
graphics.setColor(new Color(random.nextFloat(), random.nextFloat(), random.nextFloat(),1));
graphics.fillRect(k,j,1,1);
}
}
}

之后,文本将获得您想要的颜色/带有“隐藏”文本

enter image description here enter image description here

关于java - TYPE_INT_ARGB 图像未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21389063/

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