portal icon indicating copy to clipboard operation
portal copied to clipboard

BufferedImage

Open genmeblog opened this issue 1 year ago • 6 comments

Is there a way to display JVM BufferedImage object?

genmeblog avatar Feb 25 '24 21:02 genmeblog

@genmeblog, currently not directly. An easy work around would be to turn that object into a byte array then Portal should be able to render the image.

djblue avatar Feb 25 '24 23:02 djblue

What should be a structure of such byte array? Encoded png or jpg?

genmeblog avatar Feb 25 '24 23:02 genmeblog

I think either should work since I just pass the info directly to the web browser, but I've only tested png byte arrays.

djblue avatar Feb 26 '24 00:02 djblue

Ok, thanks! Do you plan to add BufferedImage in the near future?

genmeblog avatar Feb 26 '24 09:02 genmeblog

This works indeed:

(tap> (let [bufferedimage (java.awt.image.BufferedImage. 300 300 java.awt.image.BufferedImage/TYPE_INT_ARGB)
            baos (java.io.ByteArrayOutputStream.)]
        (javax.imageio.ImageIO/write bufferedimage "png" baos)
        (pview/image (.toByteArray baos))))

genmeblog avatar Feb 26 '24 09:02 genmeblog

I think for now I would rather users go through datafy to achieve this, like:

(ns user
  (:require [clojure.core.protocols :refer [Datafiable]]
            [clojure.datafy :as d]
            [portal.api :as p]
            [portal.viewer :as v])
  (:import (java.awt.image BufferedImage ImageIO)
           (java.io ByteArrayOutputStream)))

(extend-protocol Datafiable
  BufferedImage
  (datafy [^BufferedImage bufferedimage]
    (let [baos (ByteArrayOutputStream.)]
      (ImageIO/write bufferedimage "png" baos)
      (v/image (.toByteArray baos)))))

(def submit (comp p/submit d/datafy))
(add-tap #'submit)
(tap> (BufferedImage. 300 300 BufferedImage/TYPE_INT_ARGB))

Additionally, the submit function doesn't need to use datafy for dispatch, it could use another method to determine how to transform tap>'d values.

djblue avatar Feb 26 '24 18:02 djblue

Going to close this for now, feel free to open it back up if you have more questions.

djblue avatar Mar 01 '24 02:03 djblue