开发者

How to convert between color models

I am very new to image processing. I have a PNG image (read using ImageIO.read()) that yields BufferedImage.TYPE_CUSTOM when I call getType() on it.

BufferedImage bi = ImageIO.read(new URL("file:/C:/samp1.png"));
int type =bi.getType(); //TYPE_CUSTOM for samp1.png

Now I would like to convert it to one of the following models:

  1. TYPE_USHORT_GRAY
  2. TYPE_3BYTE_BGR
  3. TYPE_BYTE_GRAY
  4. TYPE_INT_RGB
  5. TYPE_INT_ARGB

The above needs to be done to process the image further using a library that recognises only the above types.

How do I convert from TYPE_CUSTOM color model to other models?

开发者_开发百科

Any help/pointers would be much appreciated. If there aren't any existing library to do this, any link/post to steps/algorithm would be great.


Try this:

public static BufferedImage convert(BufferedImage src, int bufImgType) {
    BufferedImage img= new BufferedImage(src.getWidth(), src.getHeight(), bufImgType);
    Graphics2D g2d= img.createGraphics();
    g2d.drawImage(src, 0, 0, null);
    g2d.dispose();
    return img;
}


Have you tried this?

BufferedImage rgbImg = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜