Fork me on GitHub
#humbleui
<
2024-01-27
>
chromalchemy15:01:34

I’m trying again to save a bitmap from the canvas using something like this.

(-> (ui/rect paint (ui/gap 0 0))
    (ui/image-snapshot)
    (Image/encodeToData EncodedImageFormat/PNG) 
    .getBytes)
I get a “member not found” error trying to call encodeToData . Is it because it’s deprecated? Are there newer utitlites for this? (I’m weak on java interop..)

chromalchemy15:01:45

(ns humbleui-demo.humbleui-demo
  (:require [io.github.humbleui.ui :as ui])
  (:import
   [io.github.humbleui.skija Paint Shader EncodedImageFormat Image ]
   ))

(def ui
  (let [shader (Shader/makeLinearGradient
                 (float 0)
                 (float 0)
                 (float 0)
                 (float (* 400 2 1))
                 (int-array 
                   [(unchecked-int 0xFF277da1)
                    (unchecked-int 0xFFffba08)]))
        paint  (-> (Paint.) (.setShader shader))]
    
    (ui/default-theme {}
      (ui/column
        [:stretch 0.2 
         (ui/rect paint (ui/gap 0 0))]
        
        ;; where to put this?
        (-> (ui/rect paint (ui/gap 0 0))
          (ui/image-snapshot)
          (.encodeToData EncodedImageFormat/PNG)
          .getBytes)
        ))))

(comment
  ;; example code
  (-> ui/canvas .getSurface .makeImageSnapshot)
  (-> image (.encodeToData EncodedImageFormat/PNG) .getBytes)

  (ui/start-app!
    (ui/window
      {:title "HumbleUI"}
      #'ui))
  )
; Execution error (IllegalArgumentException) at humbleui-demo.humbleui-demo/fn (REPL:29).
; No matching method encodeToData found taking 1 args for class io.github.humbleui.ui.image_snapshot.ImageSnapshot

Niki21:01:12

No, it’s because ui/image-snapshot does not do what you think it does. What do you mean “save bitmap from canvas”? What’s canvas?

chromalchemy02:01:18

Is there a way to render a jpeg or png of a rectangular element (in the humbleui window, which i was referring to as “canvas” ). Preferably constrained to specified pixel dimensions, and with optional transparency in pngs. Im a fan of your typography (primitives) taste, …and I have ptsd from css! I would like to compose and composite graphics in humblui design space, but export image assets for use in other things (email, html, etc).

Niki10:01:56

So one way would be to create in-memory Surface, get Canvas from it, pass that canvas to HumbleUI component’s -draw method, and then convert surface to image. See here: https://github.com/HumbleUI/Skija/blob/master/examples/bitmap/src/RenderToBitmap.java

Niki10:01:36

(even though example is called RenderToBitmap, there are no Skija Bitmap used :)

Niki10:01:25

If you want to “screenshot” part of your active HumbleUI window, then you need Canvas::readPixels I think. Take a look at https://github.com/HumbleUI/Skija/blob/master/examples/scenes/src/BitmapScene.java

Niki11:01:25

The way I’d do it is: create a custom HumbleUI wrapper component, wrap what you want to screenshot, inside -draw call -draw on a child, then proceed with Canvas::readPixels

Niki11:01:26

There’s a rect passed to -draw that represents component’s bounds in absolute coords

Niki11:01:40

Use that to construct bitmap and calculate srcX/srcY