今回は Android Studio と Kotlin 言語を使って静止画と動画が撮影できる基本的なカメラアプリの作り方をご紹介します。
ベースは Android Studio の公式サイトを参考にしていますが、時間をかけて調べなければわからない部分も解決済みとなっていますので、開発にあまり時間をかけたくない方や、カメラアプリ開発の初級者の方におすすめします。
なお、Android 端末は非常に多種多様ですので、相性が悪くすんなりと動かない可能性もありますが、エミュレーターおよび2種類の実機での動作確認はできていますので、万が一正しく動かない場合でも少しの修正で動くようになるはずです。
~ 目次 ~
~ 開発環境 ~
開発:Android Studio (2024.2.1 Patch 3)
言語:Kolin
使用API:Camera X
動作確認:OPPO RENO7 A(Android 13)、SHARP S5-SH(Android 11)
~ 開発の流れ ~
※編集するファイルは、MainActivity.kt、activity_main.xml、build.gradle.kts(:app)、strings.xml、AndroidManifest.xml の5種類のみです。
1.プロジェクト作成
↓
2.初期設定
↓
3.gradle ファイル編集
↓
4.マニフェストファイル編集
↓
5.レイアウトファイル編集
↓
6.string.xml ファイル編集
↓
7.MainActivity.kt を編集する(8工程)
↓
8.動作テスト
~ アプリ完成画面 ~
背面カメラからの映像の上に静止画撮影と動画撮影のボタンがあるシンプルなカメラアプリです。

<動作仕様>
実行すると背面カメラのプレビュー画面と2つのボタンが表示されます。そして「PHOTO」ボタンを押すと静止画像が撮影され、「VIDEO」ボタンを押すと動画の撮影がスタートします。動画の撮影がスタートすると「VIDEO」ボタンのラベルが「STOP」に変わり、「STOP」ボタンを押すと動画の撮影が停止します。
~ 詳細解説 ~
1.プロジェクトの作成
プロジェクトの作成画面で「Empty Views Activity」を選択して「Next」ボタンをクリックします。

プロジェクト名は自由です。開発言語は「Kotlin」を選択します。プロジェクト名の入力と開発言語の選択が終わったら「Finish」をクリックして、Android Studio がプロジェクトを作成するのを少し待ちます。

2.初期設定
プロジェクトの作成が終わったら「File」⇒「Project Structure」をクリックしてプロジェクトの設定画面を開きます。そして、右上のメニューから「Propertys」をクリックして、その中にある「Compile Sdk Version」を最新のバージョン(現在は35)に変更します。これをしないとエラーが出て実行できませんので必ず変更してください。

<ポイント>
Android Studio の下部には進捗状況を表す青いバーがあります。このバーが表示されている時は Android Studio が忙しい状態なので、コードの編集はなるべく控えた方が良いでしょう。

3.gradle ファイルの編集
「build.gradle.kts (Module:app)」ファイル内の android { } 内に buildFeatures 項目を追加します。
android {
...省略...
buildFeatures {
viewBinding = true
}
}
続けて dependencies { } 内に次の6つの項目を追加します。
dependencies {
...省略...
//追加
implementation("androidx.camera:camera-core:1.4.1")
implementation("androidx.camera:camera-camera2:1.4.1")
implementation("androidx.camera:camera-lifecycle:1.4.1")
implementation("androidx.camera:camera-video:1.4.1")
implementation("androidx.camera:camera-view:1.4.1")
implementation("androidx.camera:camera-extensions:1.4.1")
}
追加が終わったら編集画面の上部に表示される「Sync Now…」をクリックしてインポート処理が終わるのを待ちます。

4.Manifest ファイルの編集
次に Manifest ファイルに次の4項目を追加します。
<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
5.レイアウトファイルの編集
レイアウトファイル ( activity_main.xml ) にプレビュー映像を表示するために必要な PreviewView と、静止画用の撮影ボタンと動画用の撮影ボタンを追加します。また、translationZ で重なりの優先順位を設定しています。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- プレビュー表示用 -->
<androidx.camera.view.PreviewView
android:id="@+id/preview"
android:layout_width="match_parent"
android:translationZ="0dp"
android:layout_height="match_parent" />
<!-- 動画撮影ボタン -->
<Button
android:id="@+id/video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VIDEO"
android:translationZ="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<!-- 静止画撮影ボタン -->
<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PHOTO"
android:translationZ="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<画面イメージ>

6.strings.xml ファイルの編集
res フォルダ内にある values フォルダ内の string.xml ファイルで、今回作るカメラアプリのアプリ名、静止画撮影ボタンと動画撮影ボタンのラベル名を設定します。
<resources>
<!-- アプリ名 -->
<string name="app_name">Camera X Sample</string>
<!-- 静止画撮影ボタンのラベル -->
<string name="take_photo">PHOTO</string>
<!-- 動画撮影ボタンのラベル -->
<string name="start_capture">VIDEO</string>
<!-- 動画撮影ストップボタンのラベル -->
<string name="stop_capture">STOP</string>
</resources>
「Camera Xでカメラアプリを作ってみよう(言語:Kotlin)~ 前編 ~」は以上です。この続きは後編をご覧ください。
END