2016-06-29 3 views
3

に変換します。はJavaFXのイメージオブジェクトは、我々は</p> <pre><code>byte [] bytes = ------; //valid image in bytes javafx.scene.image.Image image = new Image(new ByteArrayInputStream(bytes)); </code></pre> <p>を使ってFXのImageオブジェクトを作成することができ、これはImageViewのに設定することができますバイト配列

Iはことなく反対が最初たBufferedImage(SwingFXUtils.fromFXImage(画像、NULL))に変換する必要があります。

私は直接バイトをファイルに書き込むことができます。

私は次のことを試してみた:

PixelReader pixelReader = image.getPixelReader(); 
    int width = (int)image.getWidth(); 
    int height = (int)image.getHeight(); 
    byte[] buffer = new byte[width * height * 4]; 
    pixelReader.getPixels(
      0, 
      0, 
      width, 
      height, 
      PixelFormat.getByteBgraInstance(), 
      buffer, 
      0, 
      width * 4 
    ); 

しかしバイト[]バッファを書き込むことによって生成されたファイルが有効なイメージではありません。

これについての洞察はありますか?

編集:How to get byte[] from javafx imageViewで与えられた解決策は私の質問には適用できません。 SwingFXUtilsを使ってBufferedImageに変換するのは使いたくないと明記しています。 また、イメージファイルに書き込むことができるようにバイト配列に変換したいと思います。

+3

可能な重複:// stackoverflowの.com/questions/24038524/how-to-get-byte-javafx-imageview) – Destrif

答えて

5

は、これは後で方法ですが、誰もが知るしたい場合は、この方法を試してください。

// Load the Image into a Java FX Image Object // 

Image img = new Image(new FileInputStream("SomeImageFile.png")); 

// Cache Width and Height to 'int's (because getWidth/getHeight return Double) and getPixels needs 'int's // 

int w = (int)img.getWidth(); 
int h = (int)img.getHeight(); 

// Create a new Byte Buffer, but we'll use BGRA (1 byte for each channel) // 

byte[] buf = new byte[w * h * 4]; 

/* Since you can get the output in whatever format with a WritablePixelFormat, 
    we'll use an already created one for ease-of-use. */ 

img.getPixelReader().getPixels(0, 0, w, h, PixelFormat.getByteBgraInstance(), buf, 0, w * 4); 

/* Second last parameter is byte offset you want to start in your buffer, 
    and the last parameter is stride (in bytes) per line for your buffer. */ 
[バイトを取得する方法\ [\]のJavaFX ImageViewのから?](HTTPの
関連する問題

 関連する問題