Java – when I read an image, the source raster array and the source color space component do not match
See English answers > illegalargumentexception: numbers of source raster bands and source color space components do not match for a color image exception1
myPicture = ImageIO.read(new File("./src/javaassignment1b/Deck/Ace_Diamond_1.jpg"));
I received the following error
Numbers of source Raster bands and source color space components do not match
In the research, there is a similar question. It seems to be a problem with my JPEG image (it is cutting from a larger image, which may lead to errors). I have to solve the gray problem. I don't know what about or how to implement it
Note: at first I tried to use imageicon to add to JPanel, but it didn't work, so I found that this solution caused my current problem I just started programming in Java
Editor: here is the image of link. I'm working on a Java application
I need an alternative way to add images to JPanel
Solution
Open the image using any image viewer / editor and save it in "standard" format
I don't know how images are created My guess is that you have used some fancy and complex image processing programs that you mentioned "cropping", and the program allows images to be stored in some unusual formats, and you (or did not) modify some that can be used when saving images, I don't know what these options mean (I apologize if I underestimate my familiarity with the image format and color space here)
However, the reason for the problem is that images are stored in some strange form of YPbPr or YCbCr color space (I didn't try to figure that out, but they don't support imageio by default)
My attempts at ypbpb - > RGB or ycbcb - > RGB standard color conversions have not tried to reproduce the exact original color, but you may notice that when reading Wikipedia articles: quite a few academic vapors are in the color space world
Again, it is strongly recommended that you open the image using any image viewer / editor and save it in "standard" format It is best to use PNG, because JPG is not suitable for these images Jpg is more suitable for "natural" images, such as photos For such "artificial" images, the compression must be very low to avoid artifacts
However, the image displayed by the program has almost the correct color, but the color conversion does not seem to be completely correct
import java.awt.image.BufferedImage; import java.awt.image.Raster; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class StrangeImageTest { public static void main(String[] args) throws IOException { final BufferedImage image = readImage(new File("Ace_Diamond_1.jpg")); SwingUtilities.invokelater(new Runnable() { @Override public void run() { JFrame f = new JFrame(); f.getContentPane().add(new JLabel(new ImageIcon(image))); f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } }); } static BufferedImage readImage(File file) throws IOException { return readImage(new FileInputStream(file)); } static BufferedImage readImage(InputStream stream) throws IOException { Iterator<ImageReader> imageReaders = ImageIO.getImageReadersBySuffix("jpg"); ImageReader imageReader = imageReaders.next(); ImageInputStream iis = ImageIO.createImageInputStream(stream); imageReader.setInput(iis,true,true); Raster raster = imageReader.readRaster(0,null); int w = raster.getWidth(); int h = raster.getHeight(); BufferedImage result = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); int rgb[] = new int[3]; int pixel[] = new int[3]; for (int x=0; x<w; x++) { for (int y=0; y<h; y++) { raster.getPixel(x,y,pixel); int Y = pixel[0]; int CR = pixel[1]; int CB = pixel[2]; toRGB(Y,CB,CR,rgb); int r = rgb[0]; int g = rgb[1]; int b = rgb[2]; int bgr = ((b & 0xFF) << 16) | ((g & 0xFF) << 8) | (r & 0xFF); result.setRGB(x,bgr); } } return result; } // Based on http://www.equasys.de/colorconversion.html private static void toRGB(int y,int cb,int cr,int rgb[]) { float Y = y / 255.0f; float Cb = (cb-128) / 255.0f; float Cr = (cr-128) / 255.0f; float R = Y + 1.4f * Cr; float G = Y -0.343f * Cb - 0.711f * Cr; float B = Y + 1.765f * Cb; R = Math.min(1.0f,Math.max(0.0f,R)); G = Math.min(1.0f,G)); B = Math.min(1.0f,B)); int r = (int)(R * 255); int g = (int)(G * 255); int b = (int)(B * 255); rgb[0] = r; rgb[1] = g; rgb[2] = b; } }
(Note: this can be implemented more simply. Colorconvertop can be converted from the color space used for images to RGB color space, but as mentioned above, I don't think of which color space can be used to save images. Even if I know, I must create an appropriate colorspace implementation. The first websearch result, such as ycbcrcolorspace.java, is not applicable to a given input image...)