|
21 | 21 | ### 开发环境效果
|
22 | 22 |
|
23 | 23 | 
|
| 24 | + |
| 25 | +### 和安卓Java代码互操作 |
| 26 | + |
| 27 | +```clojure |
| 28 | +(ns plain.someactivity2 |
| 29 | + (:require |
| 30 | + [clojure.tools.nrepl.server :as repl] |
| 31 | + [clojure.core.async :as a] |
| 32 | + [org.httpkit.client :as http]) |
| 33 | + (:import |
| 34 | + (android.support.v7.app AppCompatActivity) |
| 35 | + (android.util Log) |
| 36 | + (com.example.ndksample.myapplication.R$id) |
| 37 | + (com.example.ndksample.myapplication.R$layout) |
| 38 | + (android.os Handler Bundle) |
| 39 | + (org.tensorflow.contrib.android TensorFlowInferenceInterface) |
| 40 | + (org.tensorflow Operation) |
| 41 | + (android.graphics Bitmap) |
| 42 | + (android.text.method ScrollingMovementMethod) |
| 43 | + (android.view View View$OnClickListener) |
| 44 | + (android.widget Button ImageView TextView) |
| 45 | + (com.wonderkiln.camerakit CameraKitError CameraKitEvent CameraKitEventListener CameraKitImage CameraKitVideo CameraView) |
| 46 | + (java.util List) |
| 47 | + (java.util.concurrent Executor Executors) |
| 48 | + (com.mindorks.tensorflowexample TensorFlowImageClassifier Classifier)) |
| 49 | + (:gen-class |
| 50 | + :name "plain.someactivity2.MyActivity" |
| 51 | + :exposes-methods {onCreate superOnCreate |
| 52 | + onResume superOnResume |
| 53 | + onPause superOnPause |
| 54 | + onDestroy superOnDestroy} |
| 55 | + :extends android.support.v7.app.AppCompatActivity |
| 56 | + :prefix "some-")) |
| 57 | + |
| 58 | +(defn fetch [url] |
| 59 | + (http/get url)) |
| 60 | + |
| 61 | +(defonce camera-view-atom (atom nil)) |
| 62 | +(defonce classifier-atom (atom nil)) |
| 63 | +(def executor (Executors/newSingleThreadExecutor)) |
| 64 | + |
| 65 | +(defn some-onResume [^plain.someactivity2.MyActivity this] |
| 66 | + (.superOnResume this) |
| 67 | + (.start @camera-view-atom)) |
| 68 | + |
| 69 | +(defn some-onPause [^plain.someactivity2.MyActivity this] |
| 70 | + (.stop @camera-view-atom) |
| 71 | + (.superOnPause this)) |
| 72 | + |
| 73 | +(defn some-onDestroy [^plain.someactivity2.MyActivity this] |
| 74 | + (.superOnDestroy this)) |
| 75 | + |
| 76 | +(defonce this-atom (atom nil)) |
| 77 | +(defonce btn-detect-object-atom (atom nil)) |
| 78 | + |
| 79 | +(defn some-onCreate [^plain.someactivity2.MyActivity this ^android.os.Bundle bundle] |
| 80 | + (.superOnCreate this bundle) |
| 81 | + (.setContentView this com.example.ndksample.myapplication.R$layout/activity_main) |
| 82 | + (reset! this-atom this) |
| 83 | + (try |
| 84 | + (do |
| 85 | + (Log/i "repl 启动中" "...") |
| 86 | + (repl/start-server :bind "127.0.0.1" :port 6868)) |
| 87 | + (catch Exception e |
| 88 | + (Log/i "已启动" "clojure repl server"))) |
| 89 | + (let [camera-view (.findViewById this com.example.ndksample.myapplication.R$id/cameraView) |
| 90 | + _ (reset! camera-view-atom camera-view) |
| 91 | + image-view-result (.findViewById this com.example.ndksample.myapplication.R$id/imageViewResult) |
| 92 | + text-view-result (.findViewById this com.example.ndksample.myapplication.R$id/textViewResult) |
| 93 | + _ (.setMovementMethod text-view-result (ScrollingMovementMethod.)) |
| 94 | + btn-toggle-camera (.findViewById this com.example.ndksample.myapplication.R$id/btnToggleCamera) |
| 95 | + btn-detect-object (.findViewById this com.example.ndksample.myapplication.R$id/btnDetectObject) |
| 96 | + _ (reset! btn-detect-object-atom btn-detect-object)] |
| 97 | + (.addCameraKitListener camera-view |
| 98 | + (proxy [CameraKitEventListener] [] |
| 99 | + (onEvent [^CameraKitEvent camera-kit-event] |
| 100 | + (Log/i "onEvent" (str camera-kit-event)) ) |
| 101 | + (onError [^CameraKitError camera-kit-error] |
| 102 | + (Log/i "onError" (str camera-kit-error)) ) |
| 103 | + (onImage [^CameraKitImage camera-kit-image] |
| 104 | + (Log/i "onImage" "......") |
| 105 | + (let [bitmap (.getBitmap camera-kit-image) |
| 106 | + bitmap (Bitmap/createScaledBitmap bitmap 224 224 false) |
| 107 | + _ (.setImageBitmap image-view-result bitmap) |
| 108 | + results (.recognizeImage @classifier-atom bitmap)] |
| 109 | + (.setText text-view-result (.toString results))) ) |
| 110 | + (onVideo [^CameraKitVideo camera-kit-video] ))) |
| 111 | + ;; |
| 112 | + (.setOnClickListener btn-toggle-camera |
| 113 | + (proxy [View$OnClickListener] [] |
| 114 | + (onClick [^View v] |
| 115 | + (.toggleFacing camera-view)))) |
| 116 | + ;; |
| 117 | + (.setOnClickListener btn-detect-object |
| 118 | + (proxy [View$OnClickListener] [] |
| 119 | + (onClick [^View v] |
| 120 | + (.captureImage camera-view)))) |
| 121 | + ;; |
| 122 | + (do |
| 123 | + (reset! |
| 124 | + classifier-atom |
| 125 | + (TensorFlowImageClassifier/create |
| 126 | + (.getAssets this) |
| 127 | + "file:///android_asset/tensorflow_inception_graph.pb" |
| 128 | + "file:///android_asset/imagenet_comp_graph_label_strings.txt" |
| 129 | + 224 117 1 "input" "output")) |
| 130 | + (.runOnUiThread this |
| 131 | + (proxy [Runnable] [] |
| 132 | + (run [] |
| 133 | + (.setVisibility btn-detect-object View/VISIBLE)))) |
| 134 | + (Log/i "初始化TensorFlow成功!" "...")) |
| 135 | + ) |
| 136 | + ) |
| 137 | + |
| 138 | +``` |
0 commit comments