xref: /aosp_15_r20/developers/build/prebuilts/gradle/Camera2Video/kotlinApp/README.md (revision d353a188ca6ec4b5eba25b5fbd7bcb8ce61322fb)
1*d353a188SXin Li
2*d353a188SXin LiAndroid Camera2Video Sample (Kotlin)
3*d353a188SXin Li====================================
4*d353a188SXin Li
5*d353a188SXin LiThis sample shows how to record video using the new Camera2 API in Android Lollipop.
6*d353a188SXin Li
7*d353a188SXin LiIntroduction
8*d353a188SXin Li------------
9*d353a188SXin Li
10*d353a188SXin LiAndroid Lollipop introduced a new camera API, called camera2. This sample uses [CameraDevice][1]
11*d353a188SXin Liand [CameraCaptureSession][2] to record video. It also uses a custom [TextureView][3] to render the output.
12*d353a188SXin Li
13*d353a188SXin LiThe main steps are:
14*d353a188SXin Li
15*d353a188SXin Li1. Create a custom TextureView class and add it to the layout. The purpose of the custom TextureView is
16*d353a188SXin Lito be able to draw itself according to an aspect ratio, which is set via a public method. Additionally,
17*d353a188SXin Lithe `onMeasure(int widthMeasureSpec, int heightMeasureSpec)` method is overridden, using the aspect ratio.
18*d353a188SXin Li2. Implement a `TextureView.SurfaceTextureListener` on your TextureView, and override its
19*d353a188SXin Li`onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height)` method to calculate
20*d353a188SXin Lithe matrix to apply to the TextureView so the camera output fits. Use the method `setTransform(matrix)` on
21*d353a188SXin Lithe TextureView.
22*d353a188SXin Li3. Implement a [`CameraDevice.StateCallback`][4] to receive events about changes of the state of the
23*d353a188SXin Licamera device. Override its methods to set your CameraDevice instance, start the preview, and stop
24*d353a188SXin Liand release the camera.
25*d353a188SXin Li4. When starting the preview, set up the MediaRecorder to accept video format.
26*d353a188SXin Li5. Then, set up a [`CaptureRequest.Builder`][5] using `createCaptureRequest(CameraDevice.TEMPLATE_RECORD)`
27*d353a188SXin Lion your CameraDevice instance.
28*d353a188SXin Li6. Then, implement a [`CameraCaptureSession.StateCallback`][6], using the method
29*d353a188SXin Li`createCaptureSession(surfaces, new CameraCaptureSession.StateCallback(){})` on your CameraDevice instance,
30*d353a188SXin Liwhere `surfaces` is a list consisting of the surface view of your TextureView and the surface of
31*d353a188SXin Liyour MediaRecorder instance.
32*d353a188SXin Li7. Use `start()` and `stop()` methods on your MediaRecorder instance to actually start and stop the recording.
33*d353a188SXin Li8. Lastly, set up and clean up your camera device in `onResume()` and `onPause()`.
34*d353a188SXin Li
35*d353a188SXin Li
36*d353a188SXin Li[1]: https://developer.android.com/reference/android/hardware/camera2/CameraDevice.html
37*d353a188SXin Li[2]: http://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.html
38*d353a188SXin Li[3]: http://developer.android.com/reference/android/view/TextureView.html
39*d353a188SXin Li[4]: https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback.html
40*d353a188SXin Li[5]: http://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Builder.html
41*d353a188SXin Li[6]: http://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.StateCallback.html
42*d353a188SXin Li
43*d353a188SXin LiPre-requisites
44*d353a188SXin Li--------------
45*d353a188SXin Li
46*d353a188SXin Li- Android SDK 27
47*d353a188SXin Li- Android Support Repository
48*d353a188SXin Li
49*d353a188SXin LiScreenshots
50*d353a188SXin Li-------------
51*d353a188SXin Li
52*d353a188SXin Li<img src="screenshots/1-launch.png" height="400" alt="Screenshot"/>
53*d353a188SXin Li<img src="screenshots/2-record.png" height="400" alt="Screenshot"/>
54*d353a188SXin Li<img src="screenshots/3-save.png" height="400" alt="Screenshot"/>
55*d353a188SXin Li
56*d353a188SXin LiGetting Started
57*d353a188SXin Li---------------
58*d353a188SXin Li
59*d353a188SXin LiThis sample uses the Gradle build system. To build this project, use the
60*d353a188SXin Li"gradlew build" command or use "Import Project" in Android Studio.
61*d353a188SXin Li
62*d353a188SXin LiSupport
63*d353a188SXin Li-------
64*d353a188SXin Li
65*d353a188SXin Li- Google+ Community: https://plus.google.com/communities/105153134372062985968
66*d353a188SXin Li- Stack Overflow: http://stackoverflow.com/questions/tagged/android
67*d353a188SXin Li
68*d353a188SXin LiIf you've found an error in this sample, please file an issue:
69*d353a188SXin Lihttps://github.com/googlesamples/android-Camera2Video
70*d353a188SXin Li
71*d353a188SXin LiPatches are encouraged, and may be submitted by forking this project and
72*d353a188SXin Lisubmitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
73*d353a188SXin Li
74*d353a188SXin LiLicense
75*d353a188SXin Li-------
76*d353a188SXin Li
77*d353a188SXin LiCopyright 2018 The Android Open Source Project, Inc.
78*d353a188SXin Li
79*d353a188SXin LiLicensed to the Apache Software Foundation (ASF) under one or more contributor
80*d353a188SXin Lilicense agreements.  See the NOTICE file distributed with this work for
81*d353a188SXin Liadditional information regarding copyright ownership.  The ASF licenses this
82*d353a188SXin Lifile to you under the Apache License, Version 2.0 (the "License"); you may not
83*d353a188SXin Liuse this file except in compliance with the License.  You may obtain a copy of
84*d353a188SXin Lithe License at
85*d353a188SXin Li
86*d353a188SXin Lihttp://www.apache.org/licenses/LICENSE-2.0
87*d353a188SXin Li
88*d353a188SXin LiUnless required by applicable law or agreed to in writing, software
89*d353a188SXin Lidistributed under the License is distributed on an "AS IS" BASIS, WITHOUT
90*d353a188SXin LiWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
91*d353a188SXin LiLicense for the specific language governing permissions and limitations under
92*d353a188SXin Lithe License.
93