กล้อง

คุณสามารถใช้กล้อง 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.
    }
    

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

รูปภาพ

ในการจับภาพโดยใช้ Glassware ในตัวของกล้อง ให้ทำดังนี้

  1. โทร startActivityForResult(Intent, int) ที่ตั้งค่าการดำเนินการเป็น ACTION_IMAGE_CAPTURE
  2. ใน onActivityResult(int, int, android.content.Intent):
    1. ตรวจสอบว่า requestCode ตรงกับรหัสคำขอที่ใช้เมื่อ เริ่มต้นจุดประสงค์ในการจับภาพ
    2. ตรวจสอบว่า resultCode ตรงกับ RESULT_OK
    3. ดูเส้นทางไปยังภาพขนาดย่อของรูปภาพจาก ของ Intent ด้วย EXTRA_THUMBNAIL_FILE_PATH ได้หากจำเป็น
    4. เส้นทางไปยังรูปภาพเต็มมีให้บริการจาก ของ Intent ด้วย EXTRA_PICTURE_FILE_PATH เมื่อจุดประสงค์ในการจับภาพแสดงผลการควบคุมไปยัง กลาสแวร์ รูปภาพอาจไม่ได้รับการเขียนลงไฟล์อย่างสมบูรณ์ ยืนยัน ว่ามีไฟล์ภาพอยู่ หรือใช้ 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();
    }
}

วิดีโอ

การถ่ายวิดีโอโดยใช้ 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