# 最初のアプリを作る

## 新しいプロジェクトの作成

Monacaでのはじめてのアプリを開発するため、まずプロジェクトを作成しましょう。

プロジェクトは空のテンプレートをもとに、次の手順で作成します。

1. 「新しいプロジェクトを作る」ボタンをクリック
2. 「最小限のテンプレート」を選択
3. 「作成」ボタンをクリック

<figure><img src="https://3046938759-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfWrOnV1iKerkfShm9O%2Fuploads%2F37k7xDY533lvkSFHHwDY%2FScreenFlow0.gif?alt=media&#x26;token=de99a88a-a23a-4bd3-822d-3ce0c8feda89" alt=""><figcaption><p>新規プロジェクトの作成</p></figcaption></figure>

## 画面の作成

次にカメラを起動させる画面を作成していきます。

index.html の body タグ内に、カメラをスタートさせる `button` タグ と撮影した画像を表示する`img` タグを次のように記述します。

```html
<body>
    <img id="photo" height="400">
    <button id="shoot-button" onclick="shoot()">写真を撮る</button>
</body>
```

## カメラの起動処理

カメラを起動させる処理を作成します。起動処理は、JavaScriptで記述します。

index.html の `script` タグ内に、次のようにカメラを実行する処理 (shoot関数) を記述します。

```javascript
<script>
    function shoot() {
        const option = {
            destinationType: Camera.DestinationType.DATA_URL,   // URLに変換
            correctOrientation: true 
        };
          
        // カメラを起動
        navigator.camera.getPicture(onSuccess, onError, option);

        // 成功時に呼び出されるコールバック関数
        function onSuccess(data) {
            document.querySelector("#photo").src = "data:image/jpeg;base64," + data;
        }
        
        // 失敗時に呼び出されるコールバック関数
        function onError(message) {
            alert("Error:" + message);
        }
    }
</script>
```

## カメラプラグインの有効化

モバイル端末のカメラ機能を利用するため、プラグインのインストールを行います。

プラグインは、JavaScriptのコードからモバイル端末の機能を利用する際に必要となります。

1. メニューの「設定」＞「Cordovaプラグインの管理」
2. 「カメラ」プラグインを有効化します。

<figure><img src="https://3046938759-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfWrOnV1iKerkfShm9O%2Fuploads%2FkoKs4BjOAKFsKLHTrn1B%2FScreenFlow.gif?alt=media&#x26;token=ff18b53b-8c3d-4e66-b1fc-ed0d525f369b" alt=""><figcaption><p>カメラプラグインの有効化</p></figcaption></figure>

## アプリの実行

ここまでの手順で、カメラアプリは完成です。完成したカメラアプリを端末で確認していきましょう。

確認する方法としては、次の方法があります。

1. 作成したプロジェクトをAndroidまたはiOS用にビルドして確認する方法
2. iOS・Androidストア上で提供されている「Monacaデバッガー」で確認する方法

ここでは1.の方法としてAndroid用ビルドする方法と2.の「Monacaデバッガー」で確認する方法を解説します。

### Android ビルド

Android用にプロジェクトをビルドする手順は、次の手順です。

1. メニューの「ビルド」＞「Androidアプリのビルド」
2. ビルドを開始する

<figure><img src="https://3046938759-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfWrOnV1iKerkfShm9O%2Fuploads%2FTQzkqXL7yvTMVjM48pgZ%2Fandroid%E3%83%92%E3%82%99%E3%83%AB%E3%83%88%E3%82%99.gif?alt=media&#x26;token=5e5ade23-48cb-4197-8039-1f80e23eee29" alt=""><figcaption><p>Androidビルドの開始</p></figcaption></figure>

### Monaca デバッガー

次のアプリストアリンクから、Monacaデバッガーをインストールします。

{% embed url="<https://play.google.com/store/apps/details?id=mobi.monaca.debugger>" %}

&#x20;アプリにログインすると今回作成したプロジェクトが表示されます。そのプロジェクトをクリックするとカメラアプリが起動します。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ja.docs.monaca.io/build-your-first-app.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
