Swing/ColorConvertOp のバックアップソース(No.10)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/ColorConvertOp へ行く。
- 1 (2006-01-18 (水) 20:15:07)
- 2 (2006-02-27 (月) 15:34:23)
- 3 (2006-04-12 (水) 19:38:08)
- 4 (2006-05-18 (木) 17:10:13)
- 5 (2006-05-18 (木) 18:10:45)
- 6 (2007-02-23 (金) 17:20:54)
- 7 (2007-07-27 (金) 13:15:14)
- 8 (2007-08-21 (火) 12:49:19)
- 9 (2007-11-15 (木) 18:41:52)
- 10 (2008-01-10 (木) 10:39:27)
- 11 (2008-01-10 (木) 14:27:28)
- 12 (2009-10-09 (金) 20:51:35)
- 13 (2010-02-27 (土) 02:54:58)
- 14 (2010-10-14 (木) 12:54:42)
- 15 (2012-01-17 (火) 17:50:50)
- 16 (2013-02-26 (火) 19:50:44)
- 17 (2013-03-21 (木) 16:04:18)
- 18 (2013-08-01 (木) 14:03:38)
- 19 (2013-09-04 (水) 00:09:41)
- 20 (2014-04-24 (木) 15:54:59)
- 21 (2014-11-05 (水) 04:49:45)
- 22 (2014-11-25 (火) 03:03:31)
- 23 (2015-12-02 (水) 00:48:32)
- 24 (2016-06-01 (水) 19:07:19)
- 25 (2016-06-03 (金) 13:26:41)
- 26 (2016-09-01 (木) 21:38:24)
- 27 (2017-10-12 (木) 13:42:29)
- 28 (2018-12-19 (水) 21:24:39)
- 29 (2019-05-22 (水) 19:35:38)
- 30 (2020-11-15 (日) 01:57:07)
- 31 (2022-12-09 (金) 12:45:40)
TITLE:ColorConvertOpで画像をグレイスケールに変換 #navi(../) *ColorConvertOpで画像をグレイスケールに変換 [#t5e7fcf3] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2005-11-21~ 更新日:&lastmod; #contents **概要 [#p5efc4cd] ColorConvertOpを使って画像をグレイスケールに変換します。 #screenshot **サンプルコード [#a2a42159] #code{{ Image img = icon1.getImage(); BufferedImage source = new BufferedImage( img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_4BYTE_ABGR); Graphics g = source.createGraphics(); g.drawImage(img, 0, 0, this); g.dispose(); ColorConvertOp colorConvert = new ColorConvertOp( ColorSpace.getInstance(ColorSpace.CS_GRAY), null); BufferedImage destination = colorConvert.filter(source, null); icon2 = new ImageIcon(destination); }} -&jnlp; -&jar; -&zip; **解説 [#s9c1238a] 用意したアイコンから、BufferedImageを作成し、これをColorConvertOp#filterメソッドを使ってグレイスケールに変換しています。 上記のサンプルでは、ラベルをクリックすると元画像とグレイスケール画像とが切り替わるようになっています。 ---- 以下のようにGrayFilter.createDisabledImageを使った場合よりきれいに変換できるようです。 #code{{ icon2 = new ImageIcon(GrayFilter.createDisabledImage(img)); }} ---- GrayFilterの代わりに、以下のようなRGBImageFilterを継承したフィルタを使う方法もあります。 #code{{ class MyGrayFilter extends RGBImageFilter { public MyGrayFilter() { canFilterIndexColorModel = true; } public int filterRGB(int x, int y, int rgb) { int a = (rgb >> 24) & 0xff; int r = (rgb >> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; int m = (2*r+4*g+b)/7; //NTSC係数による加重平均法(近似) return new Color(m,m,m,a).getRGB(); } } FilteredImageSource fis = new FilteredImageSource(img.getSource(), new MyGrayFilter()); icon2 = new ImageIcon(createImage(fis)); }} ---- BufferedImage.TYPE_BYTE_GRAY で、BufferedImageを作成して複写してもグレイスケールに変換できますが、透過色を使用している場合はすこし注意が必要なようです(参考:[[Color to Grayscale to Binary>http://forum.java.sun.com/thread.jspa?threadID=5138820]])。 #code{{ BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY); bi.createGraphics().drawImage(img, 0, 0, this); }} **参考リンク [#wb3b736d] -[[Image Color Gray Effect : Java examples (example source code) » 2D Graphics GUI » Image>http://www.java2s.com/Code/Java/2D-Graphics-GUI/ImageColorGrayEffect.htm]] -[[opus-i | シンプル素材 テンプレート 音楽素材>http://opus-i.biz/]] -%%[[フィルタによる半透明な画像の作成>http://numata.aquasky.jp/programming/java/graphics/FilteringImage.html]]%% -[[osakana.factory - グレースケールのひみつ>http://ofo.jp/osakana/cgtips/grayscale.phtml]] -[[プログラマメモ2: グレースケール>http://programamemo2.blogspot.com/2007/08/blog-post_21.html]] **コメント [#i2aa6deb] - リンクを参考にしてRGBImageFilterを使うサンプルを修正しました。 -- [[terai]] &new{2007-08-21 (火) 13:06:10}; #comment