Error capturing picture with RNCamera in clojurescript re-natal

151 views Asked by At

I'm trying to write the following js code for taking pictures using react-native-camera in re-natal:

<RNCamera
          ref={ref => {
            this.camera = ref;
          }}
          style={styles.preview}
          type={RNCamera.Constants.Type.back}
          flashMode={RNCamera.Constants.FlashMode.on}
        />
        <View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}>
          <TouchableOpacity onPress={this.takePicture.bind(this)} style={styles.capture}>
            <Text style={{ fontSize: 14 }}> SNAP </Text>
          </TouchableOpacity>
        </View>

  takePicture = async() => {
    if (this.camera) {
      const options = { quality: 0.5, base64: true };
      const data = await this.camera.takePictureAsync(options);
      console.log(data.uri);
    }
  };

And I have the following clojurescript code:


(defn take-picture [camera]
  (go (let [picture (<! (.takePictureAsync camera {:quality 0.5 :base-64 true}))]
        (prn "uri is " (.-uri picture))
        )))

(defn camera- []
  (fn []
    [safe-area-view
     [camera {
              :ref (fn [cam] (this-as this (set! (.-Camera this) cam)))
              :style {:width 500 :height 500 :justify-content "flex-end" :align-items "center" :border-color "black" :min-height "10%"}
              :type (-> ReactNativeCamera .-RNCamera .-Constants .-Type .-back)

              }]
     [view {:style {:justify-content "center"}}
      [touchable-opacity {:on-press take-picture
                          :style {:margin 5 :background-color "#999" :padding 10 :border-radius 5}
                          }
       [text "Take Pic"]
       ]]
     ]))

But upon clicking the touchable opacity that triggers the function take-picture, I get the following error: camera.takePictureAsync is not a function What am I doing wrong?

1

There are 1 answers

0
Chris Callwait On

Need to be sure @camera-ref is created and then pass it to the function, like in this example:

(let [camera-ref (atom nil)]
  [camera {:ref #(reset! camera-ref %)}]
  ......
  {:on-press (fn []
    (let [camera @camera-ref]
      (-> (.takePictureAsync camera)
          (.then #(prn "image-captured: " %))
          (.catch #(prn "Error capturing image: " %)))))})