我需要在 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);
}
}
}
我是一名优秀的程序员,十分优秀!