คุณใช้กล้อง Glass เพื่อจับภาพรูปภาพและวิดีโอ รวมถึงแสดงสตรีมตัวอย่างของกล้องสําหรับการใช้งานที่หลากหลายได้
ภาพรวม
คุณมี 2 ตัวเลือกในการจับภาพหรือวิดีโอ
- การเรียกกิจกรรมกล้องในตัวด้วย
startActivityForResult()
ใช้ตัวเลือกนี้หากเป็นไปได้ สร้างตรรกะของคุณเองด้วย Android Camera API ปฏิบัติตามหลักเกณฑ์เหล่านี้หากคุณใช้วิธีการนี้
- ถ่ายรูปด้วยการคลิกปุ่มกล้องและวิดีโอในการคลิกยาว เหมือนกับที่ Glass ทํา
- บอกผู้ใช้ว่าถ่ายภาพหรือบันทึกวิดีโอ
- เปิดหน้าจอไว้ระหว่างจับภาพ
การแชร์กล้องกับระบบ Glass
หาก Glassware ใช้ Android API เพื่อเข้าถึงกล้อง ให้ปล่อยกล้องชั่วคราวหากเป็นไปได้หากผู้ใช้กดปุ่มกล้อง ฮาร์ดแวร์
ลบล้างเมธอด
onKeyDown()
ในกิจกรรมและสกัดกั้นKEYCODE_CAMERA
เพื่อจัดการการกดปุ่มของกล้องปล่อยกล้องและกลับไปที่
false
เพื่อระบุว่าคุณไม่ได้ใช้เหตุการณ์เพื่อให้กล้อง Glass ในตัวเริ่มทํางาน
หลังจากจับภาพหรือวิดีโอแล้ว 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 ในตัว
- เรียก
startActivityForResult(Intent, int)
พร้อมกับตั้งการดําเนินการเป็นACTION_IMAGE_CAPTURE
- ใน
onActivityResult(int, int, android.content.Intent)
- ตรวจสอบว่า
requestCode
ตรงกับโค้ดคําขอที่ใช้เมื่อเริ่ม Intent สําหรับการจับภาพ - ตรวจสอบว่า
resultCode
ตรงกับRESULT_OK
- รับเส้นทางไปยังภาพขนาดย่อของรูปภาพจากส่วนขยายของ
Intent
ด้วยคีย์EXTRA_THUMBNAIL_FILE_PATH
หากจําเป็น - ดูเส้นทางไปยังรูปภาพแบบเต็มได้จากส่วนเพิ่มเติมของ
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 ในตัว
- เรียก
startActivityForResult(Intent, int)
พร้อมกับตั้งการดําเนินการเป็นACTION_VIDEO_CAPTURE
- ใน
onActivityResult(int, int, android.content.Intent)
- ตรวจสอบว่า
requestCode
ตรงกับโค้ดคําขอที่ใช้เมื่อเริ่มความตั้งใจในการบันทึกวิดีโอ - ตรวจสอบว่า
resultCode
ตรงกับRESULT_OK
- รับเส้นทางไปยังภาพขนาดย่อของวิดีโอจากส่วนขยายของ
Intent
ด้วยคีย์EXTRA_THUMBNAIL_FILE_PATH
เพื่อแสดงตัวอย่างหากจําเป็น - คุณสามารถดูเส้นทางไปยังวิดีโอที่บันทึกไว้ได้จากคําสั่งของ
Intent
เพิ่มเติมด้วยคีย์EXTRA_VIDEO_FILE_PATH
- ตรวจสอบว่า