กล้อง

คุณใช้กล้อง Glass เพื่อจับภาพรูปภาพและวิดีโอ รวมถึงแสดงสตรีมตัวอย่างของกล้องสําหรับการใช้งานที่หลากหลายได้

ภาพรวม

คุณมี 2 ตัวเลือกในการจับภาพหรือวิดีโอ

  • การเรียกกิจกรรมกล้องในตัวด้วย startActivityForResult() ใช้ตัวเลือกนี้หากเป็นไปได้
  • สร้างตรรกะของคุณเองด้วย Android Camera API ปฏิบัติตามหลักเกณฑ์เหล่านี้หากคุณใช้วิธีการนี้

    • ถ่ายรูปด้วยการคลิกปุ่มกล้องและวิดีโอในการคลิกยาว เหมือนกับที่ Glass ทํา
    • บอกผู้ใช้ว่าถ่ายภาพหรือบันทึกวิดีโอ
    • เปิดหน้าจอไว้ระหว่างจับภาพ

การแชร์กล้องกับระบบ Glass

หาก Glassware ใช้ Android API เพื่อเข้าถึงกล้อง ให้ปล่อยกล้องชั่วคราวหากเป็นไปได้หากผู้ใช้กดปุ่มกล้อง ฮาร์ดแวร์

  1. ลบล้างเมธอด onKeyDown() ในกิจกรรมและสกัดกั้น KEYCODE_CAMERA เพื่อจัดการการกดปุ่มของกล้อง

  2. ปล่อยกล้องและกลับไปที่ false เพื่อระบุว่าคุณไม่ได้ใช้เหตุการณ์เพื่อให้กล้อง Glass ในตัวเริ่มทํางาน

  1. หลังจากจับภาพหรือวิดีโอแล้ว Glass จะกลับไปยังกิจกรรมของคุณซึ่งคุณเรียกคืนกล้องได้ใน onResume()

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_CAMERA) {
            // Stop the preview and release the camera.
            // Execute your logic as quickly as possible
            // so the capture happens quickly.
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // Re-acquire the camera and start the preview.
    }
    

การจับภาพหรือวิดีโอ

รูปภาพ

วิธีจับภาพโดยใช้ Glass Glassware ในตัว

  1. เรียก startActivityForResult(Intent, int) พร้อมกับตั้งการดําเนินการเป็น ACTION_IMAGE_CAPTURE
  2. ใน onActivityResult(int, int, android.content.Intent)
    1. ตรวจสอบว่า requestCode ตรงกับโค้ดคําขอที่ใช้เมื่อเริ่ม Intent สําหรับการจับภาพ
    2. ตรวจสอบว่า resultCode ตรงกับ RESULT_OK
    3. รับเส้นทางไปยังภาพขนาดย่อของรูปภาพจากส่วนขยายของ Intent ด้วยคีย์ EXTRA_THUMBNAIL_FILE_PATH หากจําเป็น
    4. ดูเส้นทางไปยังรูปภาพแบบเต็มได้จากส่วนเพิ่มเติมของ Intent ที่มีคีย์ EXTRA_PICTURE_FILE_PATH เมื่อ Intent จับภาพแสดงผลการควบคุมไปยัง Glasslass ระบบอาจไม่สามารถเขียนอิมเมจไปยังไฟล์ได้อย่างสมบูรณ์ ยืนยันว่ามีไฟล์ภาพอยู่หรือใช้ FileObserver เพื่อตรวจสอบไดเรกทอรีระดับบนสุด เมื่อรูปภาพขนาดเต็มพร้อมใช้งาน ให้โหลดไฟล์และนําไปใช้ใน Glassware
private static final int TAKE_PICTURE_REQUEST = 1;

private void takePicture() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
        String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH);
        String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);

        processPictureWhenReady(picturePath);
        // TODO: Show the thumbnail to the user while the full picture is being
        // processed.
    }

    super.onActivityResult(requestCode, resultCode, data);
}

private void processPictureWhenReady(final String picturePath) {
    final File pictureFile = new File(picturePath);

    if (pictureFile.exists()) {
        // The picture is ready; process it.
    } else {
        // The file does not exist yet. Before starting the file observer, you
        // can update your UI to let the user know that the application is
        // waiting for the picture (for example, by displaying the thumbnail
        // image and a progress indicator).

        final File parentDirectory = pictureFile.getParentFile();
        FileObserver observer = new FileObserver(parentDirectory.getPath(),
                FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
            // Protect against additional pending events after CLOSE_WRITE
            // or MOVED_TO is handled.
            private boolean isFileWritten;

            @Override
            public void onEvent(int event, String path) {
                if (!isFileWritten) {
                    // For safety, make sure that the file that was created in
                    // the directory is actually the one that we're expecting.
                    File affectedFile = new File(parentDirectory, path);
                    isFileWritten = affectedFile.equals(pictureFile);

                    if (isFileWritten) {
                        stopWatching();

                        // Now that the file is ready, recursively call
                        // processPictureWhenReady again (on the UI thread).
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                processPictureWhenReady(picturePath);
                            }
                        });
                    }
                }
            }
        };
        observer.startWatching();
    }
}

วิดีโอ

วิธีบันทึกวิดีโอโดยใช้ Glass Glassware ในตัว

  1. เรียก startActivityForResult(Intent, int) พร้อมกับตั้งการดําเนินการเป็น ACTION_VIDEO_CAPTURE
  2. ใน onActivityResult(int, int, android.content.Intent)
    1. ตรวจสอบว่า requestCode ตรงกับโค้ดคําขอที่ใช้เมื่อเริ่มความตั้งใจในการบันทึกวิดีโอ
    2. ตรวจสอบว่า resultCode ตรงกับ RESULT_OK
    3. รับเส้นทางไปยังภาพขนาดย่อของวิดีโอจากส่วนขยายของ Intent ด้วยคีย์ EXTRA_THUMBNAIL_FILE_PATH เพื่อแสดงตัวอย่างหากจําเป็น
    4. คุณสามารถดูเส้นทางไปยังวิดีโอที่บันทึกไว้ได้จากคําสั่งของ Intent เพิ่มเติมด้วยคีย์ EXTRA_VIDEO_FILE_PATH