作成途中
void test(){ try { BufferedImage bufferedImage2 = ImageIO.read(new File("data.java")); int w = bufferedImage2.getWidth(); int h = bufferedImage2.getHeight(); WritableRaster raster = bufferedImage2.getRaster(); System.out.println("getNumBands:" + raster.getNumBands());//4が表示 int []bufferSrc = new int[w * h]; for(int y = 1; y < 100; y++){//rasterを返した描画(これで、緑の線が描けた。) raster.setPixel(10, y, new int[]{0,255,0,255});// R,G,B,A } //raster.setSamples(0, 0, w, h, 0, bufferSrc);//setPixelではbufferSrcの構造が合わない。 //this.bufferedImage2.setData(raster);//左の記述は調査中 Graphics g = bufferedImage2.getGraphics(); g.setColor(Color.RED); g.drawLine(5, 5, 10, 10);//通常の描き方 //次のように、int配列のbufferSrcを直接にセットすることや、setRGBで配列に取得することも可能 bufferedImage2.setRGB(0, 0, w, h, bufferSrc, 0, w);//配列をイメージに書き込む } catch (IOException e) { e.printStackTrace(); } } // 以下も配列から生成する方法であるが、検討中(参照:http://nodamushi.hatenablog.com/entry/20110912/1315859917) public static BufferedImage createImage(int[] data,int width,int height,int alphaMask,int redMask,int greenMask,int blueMask){ int[] mask = {redMask,greenMask,blueMask,alphaMask}; DataBuffer buffer = new DataBufferInt(data, width*height); WritableRaster raster = Raster.createPackedRaster(buffer,width,height,width,mask,new Point()); ColorModel model = new DirectColorModel(32, redMask, greenMask, blueMask, alphaMask); return new BufferedImage(model, raster, false, null); }
canvas.setOnMouseClicked(e->{ File file = fc.showOpenDialog(primaryStage); if(file != null) setImage( file ); try { URL url = file.toURI().toURL(); Image imageSrc = new Image(url.toExternalForm());//ファイルのURLからイメージを取得 GraphicsContext gc = canvas.getGraphicsContext2D();//上記キャンバスへの描画ツール取得 gc.drawImage(imageSrc , 0, 0);//描く PixelReader pixelReader = imageSrc .getPixelReader(); double w = imageSrc.getWidth(); double h = imageSrc.getHeight(); int targetRgb = pixelReader.getArgb((int)e.getX(),(int) e.getY());//この点のRGBを取得 //pixelReader.getPixels(x, y, w, h, pixelformat, buffer, scanlineStride); } catch (Exception err) { err.printStackTrace(); } });
void test(){ try { BufferedImage bufferedImage2 = ImageIO.read(new File("data.java")); int w = bufferedImage2.getWidth(); int h = bufferedImage2.getHeight(); WritableImage writavleImage = new WritableImage(w , h); javafx.scene.image.Image img = SwingFXUtils.toFXImage(bufferedImage2, writavleImage); GraphicsContext gc = canvas.getGraphicsContext2D();//上記キャンバスへの描画ツール取得 gc.drawImage(img , 0, 0); } catch (IOException e) { e.printStackTrace(); } }
RL url = new file("data.jpg").toURI().toURL(); javafx.scene.image.Image imageSrc = new Image(url.toExternalForm()); BufferedImage bufImgSrc = new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB); // imageSrcからjavafx.scene.image.ImageenoにのbufImgSrcへ変換 bufImgSrc = SwingFXUtils.fromFXImage(imageSrc, bufImgSrc);
void test() throws Exception{ File file = new File("data.jpg"); URL url = file.toURI().toURL(); javafx.scene.image.Image imageSrc = new Image(url.toExternalForm()); int w = (int)imageSrc.getWidth(); int h = (int)imageSrc.getHeight(); System.out.printf("Image size w:%d, h:%d \n", w, h); // JavaFx からのイメージ用配列(int)取得 WritablePixelFormat<IntBuffer> writablePixelFormat = WritablePixelFormat.getIntArgbInstance(); int[] bufferSrc = new int[w * h]; this.pixelReader.getPixels(0, 0, w, h, writablePixelFormat, bufferSrc, 0, w); // イメージ用配列(int)からBufferedImageを生成 BufferedImage bufferedImage2 = new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB); bufferedImage2.setRGB(0, 0, w, h, bufferSrc, 0, w);//配列をイメージに書き込む ImageIO.write(bufferedImage2, "png", new File("T.png"));//検証でファイル保存 }