gpt4 book ai didi

java - 如何在 j 帧上绘制图像的各个 RGB 分量

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:28 25 4
gpt4 key购买 nike

我需要在 Jframe 上打印该程序的输出(即三个图像)...代码如下...我可以将其存储在 netbeans 的“test”文件夹中,但无法显示它..

import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;

public class RGBSpliter {
static BufferedImage image;
static BufferedImage redImage, greenImage, blueImage;
static final int TYPE = BufferedImage.TYPE_INT_ARGB;

public static void main(String[] args) throws Exception {
image = ImageIO.read(new File("F:\\Images\\animated\\008.jpg"));
int w = image.getWidth();
int h = image.getHeight();
redImage = new BufferedImage(w, h, TYPE);
greenImage = new BufferedImage(w, h, TYPE);
blueImage = new BufferedImage(w, h, TYPE);
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
int pixel = image.getRGB(x, y);
int alpha_mask = pixel & 0xff000000;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
redImage.setRGB(x, y, alpha_mask | (red << 16));
greenImage.setRGB(x, y, alpha_mask | (green << 8));
blueImage.setRGB(x, y, alpha_mask | blue);
}
String format = "png";
ImageIO.write(redImage, format, new File("image_red.png"));
ImageIO.write(greenImage, format, new File("image_green.png"));
ImageIO.write(blueImage, format, new File("image_blue.png"));
}
}

最佳答案

你的代码对我有用:

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.net.URL;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class RGBSpliter {
static BufferedImage image;
static BufferedImage redImage, greenImage, blueImage;
static final int TYPE = BufferedImage.TYPE_INT_ARGB;

public static void main(String[] args) throws Exception {
String imgPath =
"https://duke.kenai.com/china/.Midsize/DragonGoldenMedium.jpg.png";
URL imgUrl = new URL(imgPath);
image = ImageIO.read(imgUrl);


int w = image.getWidth();
int h = image.getHeight();
redImage = new BufferedImage(w, h, TYPE);
greenImage = new BufferedImage(w, h, TYPE);
blueImage = new BufferedImage(w, h, TYPE);
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int pixel = image.getRGB(x, y);
int alpha_mask = pixel & 0xff000000;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
redImage.setRGB(x, y, alpha_mask | (red << 16));
greenImage.setRGB(x, y, alpha_mask | (green << 8));
blueImage.setRGB(x, y, alpha_mask | blue);
}
}
// String format = "png";

Image[] images = {image, redImage, greenImage, blueImage};
for (Image localImage : images) {
ImageIcon icon = new ImageIcon(localImage);
JOptionPane.showMessageDialog(null, icon);
}
}
}

关于java - 如何在 j 帧上绘制图像的各个 RGB 分量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21925791/

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