Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Trang này mô tả cách chụp ảnh chất lượng cao bằng CameraX. Bạn thực hiện việc này bằng lớp ImageCapture và các phương thức liên kết của lớp đó.
Khái niệm chính
Sau đây là các khái niệm chính được thảo luận trong tài liệu này:
Phương thức lưu trữ: Bạn có thể chụp ảnh vào vùng đệm trong bộ nhớ hoặc trực tiếp vào tệp.
Trình thực thi:ImageCapture sử dụng trình thực thi để xử lý lệnh gọi lại và các thao tác I/O. Bạn có thể tuỳ chỉnh các trình thực thi này để có hiệu suất và khả năng kiểm soát tốt hơn.
Chế độ chụp: Bạn có thể định cấu hình chế độ chụp để tối ưu hoá độ trễ hoặc chất lượng hình ảnh.
Phương thức lưu trữ
Có hai cách để chụp ảnh bằng ImageCapture. Mỗi hàm sử dụng một phương thức nạp chồng của ImageCapture.takePicture():
Điều này rất hữu ích khi xử lý hoặc phân tích hình ảnh theo thời gian thực.
Trình thực thi
Khi gọi takePicture, bạn sẽ truyền một Executor và một hàm OnImageCapturedCallback hoặc OnImageSavedCallback. Executor chạy lệnh gọi lại và xử lý mọi hoạt động đầu vào/đầu ra (IO) thu được.
Chụp ảnh
Để chụp ảnh, bạn thiết lập máy ảnh rồi gọi takePicture.
Sau khi bạn định cấu hình máy ảnh, hãy gọi takePicture() để chụp ảnh.
Ví dụ này minh hoạ cách sử dụng takePicture() để lưu hình ảnh vào ổ đĩa:
Kotlin
funonClick(){valoutputFileOptions=ImageCapture.OutputFileOptions.Builder(File(...)).build()imageCapture.takePicture(outputFileOptions,cameraExecutor,object:ImageCapture.OnImageSavedCallback{overridefunonError(error:ImageCaptureException){// insert your code here.}overridefunonImageSaved(outputFileResults:ImageCapture.OutputFileResults){// insert your code here.}})}
Java
publicvoidonClick(){ImageCapture.OutputFileOptionsoutputFileOptions=newImageCapture.OutputFileOptions.Builder(newFile(...)).build();imageCapture.takePicture(outputFileOptions,cameraExecutor,newImageCapture.OnImageSavedCallback(){@OverridepublicvoidonImageSaved(ImageCapture.OutputFileResultsoutputFileResults){// insert your code here.}@OverridepublicvoidonError(ImageCaptureExceptionerror){// insert your code here.}});}
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[],[],null,["# Capture an image\n\nThis page describes how to capture high-quality images with CameraX. You do so\nwith the [`ImageCapture`](/reference/androidx/camera/core/ImageCapture) class and its associated methods.\n| **Note:** This workflow accommodates the 3A features: auto-white-balance, auto-exposure, and auto-focus. It also supports basic manual camera controls.\n\nKey concepts\n------------\n\nThe following are the primary concepts discussed in this document:\n\n- **[Storage method](#capture):** You can capture images either to an in-memory buffer or directly to a file.\n- **[Executors](#executors):** `ImageCapture` uses executors for handling callbacks and I/O operations. You can customize these executors for better performance and control.\n- **[Capture Modes](/media/camera/camerax/take-photo/options):** You can configure the capture mode to optimize for either latency or image quality.\n\n### Storage method\n\nThere are two ways to capture images with\n`ImageCapture`. They each use an overload of `ImageCapture.takePicture()`:\n\n- **File:** Use [`takePicture(OutputFileOptions, Executor,\n OnImageSavedCallback)`](/reference/androidx/camera/core/ImageCapture#takePicture(androidx.camera.core.ImageCapture.OutputFileOptions,%20java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageSavedCallback)) to save the captured image directly to a file on\n disk.\n\n - This is the most common way to capture photos.\n- **In-Memory:** Use [`takePicture(Executor,\n OnImageCapturedCallback)`](/reference/androidx/camera/core/ImageCapture#takePicture(java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageCapturedCallback)) to receive an in-memory buffer of the captured\n image.\n\n - This is useful for real-time image processing or analysis.\n\n### Executors\n\nWhen you call `takePicture`, you pass an [`Executor`](/reference/java/util/concurrent/Executor) and either a\n`OnImageCapturedCallback` or `OnImageSavedCallback` function. The `Executor`\nruns the callback and handles any resulting IO.\n| **Note:** You can set the default executor for IO tasks with [`ImageCapture.Builder.setIoExecutor(Executor)`](/reference/androidx/camera/core/ImageCapture.Builder#setIoExecutor(java.util.concurrent.Executor)). If you don't set a default, CameraX uses to an internal IO executor.\n\nTake photo\n----------\n\nTo take a photo, you set up the camera and then call `takePicture`.\n\n### Set up the camera\n\nTo set up the camera, create a [`CameraProvider`](/media/camera/camerax/preview#get-provider). Then, create an\n`ImageCapture` object. Use [`ImageCapture.Builder()`](/reference/androidx/camera/core/ImageCapture.Builder): \n\n### Kotlin\n\n val imageCapture = ImageCapture.Builder()\n .setTargetRotation(view.display.rotation)\n .build()\n\n cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, imageCapture, preview)\n\n### Java\n\n ImageCapture imageCapture =\n new ImageCapture.Builder()\n .setTargetRotation(view.getDisplay().getRotation())\n .build();\n\n cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, imageCapture, preview);\n\n| **Note:** [`bindToLifecycle()`](/reference/androidx/camera/lifecycle/ProcessCameraProvider#bindToLifecycle(androidx.lifecycle.LifecycleOwner,%20androidx.camera.core.CameraSelector,%20androidx.camera.core.UseCase...)) returns a [`Camera`](/reference/androidx/camera/core/Camera) object. See the [Configuration options guide](/training/camerax/configuration#camera-output) for more on controlling camera output, such as zoom and exposure.\n\n### Take a picture\n\nAfter you configure the camera, call `takePicture()` to capture an image.\nThis example demonstrates how to use [`takePicture()`](/reference/androidx/camera/core/ImageCapture#takePicture(androidx.camera.core.ImageCapture.OutputFileOptions,%20java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageSavedCallback)) to save an image\nto disk: \n\n### Kotlin\n\n fun onClick() {\n val outputFileOptions = ImageCapture.OutputFileOptions.Builder(File(...)).build()\n imageCapture.takePicture(outputFileOptions, cameraExecutor,\n object : ImageCapture.OnImageSavedCallback {\n override fun onError(error: ImageCaptureException)\n {\n // insert your code here.\n }\n override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {\n // insert your code here.\n }\n })\n }\n\n### Java\n\n public void onClick() {\n ImageCapture.OutputFileOptions outputFileOptions =\n new ImageCapture.OutputFileOptions.Builder(new File(...)).build();\n imageCapture.takePicture(outputFileOptions, cameraExecutor,\n new ImageCapture.OnImageSavedCallback() {\n @Override\n public void onImageSaved(ImageCapture.OutputFileResults outputFileResults) {\n // insert your code here.\n }\n @Override\n public void onError(ImageCaptureException error) {\n // insert your code here.\n }\n }\n );\n }\n\nHere are the key points about this snippet:\n\n- The [`ImageCapture.OutputFileOptions`](/reference/androidx/camera/core/ImageCapture.OutputFileOptions) lets you configure save location and metadata.\n - Here, the `OutputFileOptions.Builder()` uses a `File` object to determine the save location.\n- The `takePicture()` function captures the image asynchronously using the provided options and executor.\n- The [`OnImageSavedCallback`](/reference/androidx/camera/core/ImageCapture#takePicture(androidx.camera.core.ImageCapture.OutputFileOptions,%20java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageSavedCallback)) provides callbacks for success and failure.\n - The `onImageSaved()` callback handles successful image capture and provides access to the [saved image results](/reference/androidx/camera/core/ImageCapture.OutputFileResults).\n - The `onError()` callback handles image capture errors.\n\nAdditional options\n------------------\n\nSee the [Configure for optimization, flash, and file format guide](/media/camera/camerax/take-photo/options) for extra\nways you can configure `ImageCapture`.\n\nFurther resources\n-----------------\n\nTo learn more about CameraX, consult the following resources:\n\n### Codelab\n\n\n- [Getting Started with CameraX](https://codelabs.developers.google.com/codelabs/camerax-getting-started)\n\n### Code sample\n\n- \n- [CameraX sample apps](https://github.com/android/camera-samples/)"]]