gpt4 book ai didi

java - 使用java渲染jpg文件

转载 作者:行者123 更新时间:2023-12-01 19:56:40 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的代码来编写红色的 100x100 jpg
由于某种原因颜色不正确,
我只设置颜色红色:

renderdImg.setRGB(x, y, Color.RED.getRGB());

但最终图像呈紫色,我做错了什么?

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageWriter {

public static void main(String[] args) throws IOException {
String fileName = "red_100.jpg";
String filePath = "c:\\temp\\";
int width = 100;
int height = 100;
BufferedImage renderdImg = new BufferedImage(width,height, BufferedImage.TYPE_INT_ARGB);
for(int x=0;x< width; x++) {
for(int y=0;y<height; y++) {
renderdImg.setRGB(x, y, Color.RED.getRGB());
}}
File fileToWrite = new File(filePath + fileName);
ImageIO.write(renderdImg, "jpg", fileToWrite);
}
}

最佳答案

将图像类型设置为BufferedImage.TYPE_INT_RGB,它应该变得偏红:

BufferedImage renderdImg = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);

关于java - 使用java渲染jpg文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49459178/

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