DIAVIS-wiki
 [[Java]]
 
 *BufferedImage [#m2a843f9]
 BufferedImage image = new BufferedImage( imgw, imgh, BufferedImage.TYPE_INT_ARGB );
 自分の環境で,以下のように4000px×4000pxでOutOfMemoryErrorが発生.3000px3000pxまではエラー発生しないことを確認済み.ただし,画像を準備しただけなので,そのほかにもメモリを使うと無理かも.
 
 > &color(#AA0000,#FFFFFF){Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space};
 > &color(#AA0000){Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space};
 
 ヒープサイズは指定できるが,増やしすぎるとGC(ガーベッジコレクション)の時間が増加するみたい.詳しくは,以下を参照
 :浅煎り珈琲 Java アプリケーション入門 Garbage Collection|http://msugai.fc2web.com/java/perform/garbage.html
 
 *画像のファイルへの書き出し [#ma1e7f4d]
 :static boolean write(RenderedImage im, String formatName, File output) |File に指定された形式をサポートする、任意の ImageWriter を使用してイメージを書き込みます。 
 :static boolean write(RenderedImage im, String formatName, ImageOutputStream output)|ImageOutputStream に、指定された形式をサポートする任意の ImageWriter を使用してイメージを書き込みます。 
 :static boolean write(RenderedImage im, String formatName, OutputStream output)|OutputStream に指定された形式をサポートする、任意の ImageWriter を使用してイメージを書き込みます。
 
  BufferedImage image = new BufferedImage( imgw, imgh, BufferedImage.TYPE_INT_ARGB );
 
 以下は書き出す際の例.ファイル名は保存時の日時を使用.
  GregorianCalendar calendar = new GregorianCalendar();
 	String time; //for filename
 	time = "" + (calendar.get(Calendar.MONTH)+1)
 						+ calendar.get(Calendar.DATE)
 						+ calendar.get(Calendar.HOUR)
 						+ calendar.get(Calendar.MINUTE)
 						+ calendar.get(Calendar.SECOND);
 	try{
 		ImageIO.write(image, "jpeg", new File("capture/" + time + ".jpeg")); //jpegで書き出す場合
 	} catch (Exception write_error){
 		System.out.println("at ImageIO.write : " + write_error);
 	}
 
 BMPで書き出す場合は以下のように記述
  ImageIO.write(image, "BMP", new File("capture/" + time + ".BMP"));
 ただし,BMPでは透明色に対応していないので,BufferedImageインスタンス化するさいの形式を,
 BufferedImage.TYPE_INT_ARGBからBufferedImage.TYPE_INT_RGBにしないと失敗する.
 BufferedImage.TYPE_INT_RGBでも,透明な色での描画は可能.
 
 
 *登録されている非公式な形式の名前をリスト [#u304f036]
 以下のメソッドで取得可能
  String[] readableFormatNames = ImageIO.getReaderFormatNames();
  String[] writableFormatNames = ImageIO.getWriterFormatNames();
 結果は,
 :getReaderFormatNames|BMP,bmp,jpeg,wbmp,gif,JPG,png,jpg,WBMP,JPEG
 :getWriterFormatNames|BMP,jpeg,bmp,wbmp,JPG,png,jpg.PNG,JPEG,WBMP
トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS