---
# System prepended metadata

title: 影像辨識

---

# 影像辨識

產生圖像資料庫

``` private boolean setupAugmentedImageDatabase(Config config) {
        AugmentedImageDatabase augmentedImageDatabase;
        // There are two ways to configure an AugmentedImageDatabase:  兩種方法配置:AugmentedImageDatabase
        // 1. Add Bitmap to DB directly                                直接將圖添加到數據庫
        // 2. Load a pre-built AugmentedImageDatabase                  加載預建的 AugmentedImageDatabase
        // Option 2) has
        // * shorter setup time                                        設置時間更短
        // * doesn't require images to be packaged in apk.             不需要將圖像打包為apk
        if (useSingleImage) {
            Bitmap augmentedImageBitmap = loadAugmentedImageBitmap();
            if (augmentedImageBitmap == null) {
                return false;
            }
            augmentedImageDatabase = new AugmentedImageDatabase(session);
            augmentedImageDatabase.addImage("70", augmentedImageBitmap);   //新增圖像
           // augmentedImageDatabase.addImage("default", augmentedImageBitmap);
            // If the physical size of the image is known, you can instead use:
            // augmentedImageDatabase.addImage("image_name", augmentedImageBitmap, widthInMeters);
            // This will improve the initial detection speed. ARCore will still actively estimate the
            // physical size of the image as it is viewed from multiple viewpoints.
        } else {
            // This is an alternative way to initialize an AugmentedImageDatabase instance,  //初始化AugmentedImageDatabase
            // load a pre-existing augmented image database.                                 //加載預建的 AugmentedImageDatabase
            try (InputStream is = getAssets().open("floor.imgdb")) {
                augmentedImageDatabase = AugmentedImageDatabase.deserialize(session, is);
            } catch (IOException e) {
                Log.e("database", "IO exception loading augmented image database.", e);
                return false;
            }
        }
        config.setAugmentedImageDatabase(augmentedImageDatabase);
        return true;
    }

```

執行的動作
``` private void drawAugmentedImages(
            Frame frame, float[] projmtx, float[] viewmtx, float[] colorCorrectionRgba) {
        Collection<AugmentedImage> updatedAugmentedImages =
                frame.getUpdatedTrackables(AugmentedImage.class);
        // Iterate to update augmentedImageMap, remove elements we cannot draw.
        for (AugmentedImage augmentedImage : updatedAugmentedImages) {
            switch (augmentedImage.getTrackingState()) {
                case PAUSED://靜止圖片 但人在動
                    // When an image is in PAUSED state, but the camera is not PAUSED, it has been detected,
                    // but not yet tracked.
                    if(augmentedImage.getIndex() == 1) {
                        Log.e("pausedR:", "pausedRok");
                        bug = "1" ;
                        check="1";
                        blocation = new LatLng(24.98970994546, 121.54632728635);
                        NaviDo();
                        break;
                    }
                    if(augmentedImage.getIndex() == 0){
                        Log.e("pausedS:", "pausedSok");
                        bug = "1" ;
                        check="1";
                        blocation = new LatLng(24.98992479329, 121.54658377259);
                        NaviDo();
                        break;
                    }
                    String text = String.format("Detected Image %d", augmentedImage.getIndex());
                    messageSnackbarHelper.showMessage(this, text);
                    break;
                case TRACKING://追蹤
                    // Have to switch to UI Thread to update View.
                    // Create a new anchor for newly found images.
                    break;
                case STOPPED://停止
                    augmentedImageMap.remove(augmentedImage.getIndex());
                    break;
            }
        }
    }

```

偵錯
```
 private void configureSession() {
        Config config = new Config(session);
       // config.setFocusMode(Config.FocusMode.AUTO);
        if (!setupAugmentedImageDatabase(config)) {
            messageSnackbarHelper.showError(this, "Could not setup augmented image database");
        }
        session.configure(config);
    }
    
```

從AugmentedImageDatabase找到圖片
```
 private Bitmap loadAugmentedImageBitmap() {
        try (
                InputStream is = getAssets().open("70.jpg")) {
                return BitmapFactory.decodeStream(is);
        } catch (IOException e) {
                Log.e("ImageBit", "IO exception loading augmented image bitmap.", e);
        }
                return null;
    }
```