如何绘制j帧上图像的单个RGB分量
我需要在Jframe上打印此程序的输出(即三个图像)...代码如下...我可以将其存储在“test “的NetBeans的文件夹,但只是不能显示它..如何绘制j帧上图像的单个RGB分量
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);
}
}
}
__我们不能在JFrame上的期望位置打印它们,例如这个JFrame f = new JFrame(); f.setTitle(“RGB”); f.setSize(new Dimension(1360,760)); f.setVisible(true); – user3335751
@ user3335751:您可以像在任何其他图像一样显示它们在JFrame中:作为JLabel中的ImageIcons或在JPanel的“paintComponent(...)”方法中绘制的图像。 –
完成..使用油漆组件,并得到所需的输出......感谢您的帮助 – user3335751
'frame.add(新的JLabel(新的ImageIcon(redImage)))'...? – MadProgrammer
不是所有的位移都比它需要的更复杂吗?可以是'redImage.setRGB(x,y,pixel&0xffff0000); greenImage.setRGB(x,y,pixel&0xff00ff00); blueImage.setRGB(x,y,pixel&0xff0000ff);' –
@MadProgrammer_我们试过了你的代码。但它没有工作。图像没有显示在jFrame中。请,我急需你的帮助。 – user3335751