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