קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
בדף הזה נסביר איך לצלם תמונות באיכות גבוהה באמצעות CameraX. עושים זאת באמצעות הכיתה ImageCapture והשיטות המשויכות אליה.
מושגים מרכזיים
אלה המושגים העיקריים שמפורטים במסמך הזה:
שיטת האחסון: אפשר לצלם תמונות למאגר בזיכרון או ישירות לקובץ.
מפעילים:ImageCapture משתמש במפעילים לטיפול בקריאות חזרה (callbacks) ובפעולות קלט/פלט. אפשר להתאים אישית את המבצעים האלה כדי לשפר את הביצועים ואת השליטה.
מצבי צילום: אפשר להגדיר את מצב הצילום כך שתתבצע אופטימיזציה לפי זמן האחזור או לפי איכות התמונה.
שיטת האחסון
יש שתי דרכים לצלם תמונות באמצעות ImageCapture. כל אחת מהן משתמשת בעומס יתר של ImageCapture.takePicture():
האפשרות הזו שימושית לעיבוד או לניתוח תמונות בזמן אמת.
מבצעים
כשקוראים לפונקציה takePicture, מעבירים Executor ופונקציית OnImageCapturedCallback או OnImageSavedCallback. הפונקציה Executor מפעילה את פונקציית הקריאה החוזרת ומטפלת בכל פעולות הקלט/פלט שנובעות ממנה.
צילום תמונה
כדי לצלם תמונה, מגדירים את המצלמה ומפעילים את takePicture.
אחרי שמגדירים את המצלמה, קוראים ל-takePicture() כדי לצלם תמונה.
בדוגמה הזו מוסבר איך משתמשים ב-takePicture() כדי לשמור תמונה בדיסק:
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.}});}
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-26 (שעון UTC).
[null,null,["עדכון אחרון: 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/)"]]