ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
カメラ を起点とした画像データの処理 / Android
日時: 2016/11/08 12:37
名前: lightbox






目的別のまとめ
カメラを起点とした画像データの処理 / Android

画面は、ViewFlipper で複数の画面として動作するようにしています。

1) 初期画面 	: カメラ
2) 次画面 	: ImageView

カメラ処理部分は以下を参照して下さい
Android : 画像関連のテスト用カメラアプリ
拡張子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="lightbox.sep.fire1.MainActivity"
    android:padding="16dp">

    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewFlipper">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="ギャラリー"
                    android:id="@+id/galleryButton"
                    android:layout_weight="1"/>

                <Button
                    android:text="表示"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageButton"
                    android:layout_weight="1"/>


            </LinearLayout>

            <SurfaceView
                android:layout_width="match_parent"
                android:id="@+id/cameraView"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="戻る"
                    android:id="@+id/backButton"
                    android:layout_weight="1"/>

                <Button
                    android:text="Firebase より"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/downloadButton"
                    android:layout_weight="1"/>

            </LinearLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imageView"/>

        </LinearLayout>

    </ViewFlipper>

</LinearLayout>
メンテナンス

MainActivity ( No.1 )
日時: 2016/11/08 14:35
名前: lightbox


日時: 2016/11/08 14:35
名前: lightbox
★ カメラ画像を ImageView に表示
★ カメラの画面に戻る
★ ギャラリーに保存
★ Firebase より画像を取得する

☆ 保存用内部ストレージパス取得
拡張子:
public class MainActivity extends AppCompatActivity {

	public static int FIRST_PAGE = 0;
	public static int SECOND_PAGE = 1;
	static long MAX_SIZE_BYTE = 1024 * 100;

	private ProgressDialog progress;
	private MyCamera camera;

	private ViewFlipper viewFlipper;
	private ImageView imageView;

	// Firebase storage
	private FirebaseStorage storage;
	private StorageReference storageRef;
	private StorageReference imageRef;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		camera = new MyCamera(MainActivity.this);

		viewFlipper = (ViewFlipper) MainActivity.this.findViewById(R.id.viewFlipper);
		viewFlipper.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));
		imageView = (ImageView) MainActivity.this.findViewById(R.id.imageView);

		// ダウンロードする画像ファイルのあるフォルダ
		storage = FirebaseStorage.getInstance();
		storageRef = storage.getReferenceFromUrl("gs://freebase-654b7.appspot.com/image");

		// *************************************
		// カメラ画像を ImageView に表示
		// *************************************
		Button buttonDisplay = (Button) MainActivity.this.findViewById(R.id.imageButton);
		buttonDisplay.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {

				camera.getCamera().takePicture(null, null, new Camera.PictureCallback() {
					@Override
					public void onPictureTaken(byte[] bytes, Camera camera) {

						// バイト配列を Bitmap に変換( 内容は既に jpeg )
						Bitmap image1 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
						// 90度回転
						int width = image1.getWidth();
						int height = image1.getHeight();
						Matrix matrix = new Matrix();
						matrix.postRotate (90);
						Bitmap image2 = Bitmap.createBitmap (image1, 0, 0, width, height, matrix, true);

						// 環境依存(これが無いと落ちるデバイスもある)
						imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

						// imageView へ表示
						imageView.setImageBitmap(image2);

						// 次画面へ移動
						viewFlipper.setDisplayedChild(SECOND_PAGE);

					}
				});

			}
		});

		// *************************************
		// カメラの画面に戻る
		// *************************************
		Button buttonBack = (Button) MainActivity.this.findViewById(R.id.backButton);
		buttonBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {

				viewFlipper.setDisplayedChild(FIRST_PAGE);

				// カメラ表示を再開
				camera.getCamera().startPreview();

			}
		});

		// *************************************
		// ギャラリーに保存
		// *************************************
		Button galleryButton = (Button) MainActivity.this.findViewById(R.id.galleryButton);
		galleryButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i("lightbox", "ギャラリーに保存");

				// 撮影
				camera.getCamera().takePicture(new Camera.ShutterCallback() {
					// シャッター音を鳴らす
					@Override
					public void onShutter() {
					}
				}, null, new Camera.PictureCallback() {
					@Override
					public void onPictureTaken(byte[] data, Camera camera) {

						// カメラから jpeg 画像データが取得できた
						if (data != null) {

							// 保存用のパスを取得
							String imagePath = buildPath("firebase", "jpg");
							if (imagePath == null) {
								Toast.makeText(MainActivity.this,
									"ディレクトリを作成できませんでした",
									Toast.LENGTH_SHORT)
									.show();
							} else {

								// 内部ストレージに保存
								FileOutputStream jpg;
								try {
									jpg = new FileOutputStream(imagePath);
									jpg.write(data);
									jpg.close();

									// ギャラリーに反映
									MediaScannerConnection.scanFile(
										MainActivity.this,
										new String[]{imagePath},
										new String[]{"image/jpg"},
										null);

								} catch (Exception e) {
									e.printStackTrace();
								}

							}

						}

						// カメラの表示が止まるので、イベントの引数のカメラでカメラ開始
						camera.startPreview();

					}
				});

			}
		});

		// *************************************
		// Firebase より画像を取得する
		// *************************************
		Button buttonDownload = (Button) MainActivity.this.findViewById(R.id.downloadButton);
		buttonDownload.setAllCaps(false);	// ボタンの文字に小文字を使用する
		buttonDownload.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {

				imageRef = storageRef.child("sworc.png");
				// ダウンロード
				imageRef.getBytes(MainActivity.MAX_SIZE_BYTE)
					.addOnSuccessListener(new OnSuccessListener<byte[]>() {
						@Override
						public void onSuccess(byte[] bytes) {
							if (bytes != null) {
								Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
								imageView.setImageBitmap(image);
							}
						}
					}).addOnFailureListener(new OnFailureListener() {
					@Override
					public void onFailure(@NonNull Exception e) {
						Log.i("lightbox","データ取得に失敗しました");
						e.printStackTrace();
						Toast.makeText(
							MainActivity.this,
							"Firebase からのダウンロードに失敗しました",
							Toast.LENGTH_LONG)
							.show();
					}
				});

			}
		});


	}

	// *************************************
	// 保存用内部ストレージパス取得
	// *************************************
	private String buildPath(String folder, String ext) {

		String path = null;

		// 内部ストレージにフォルダを作成
		String TargetRootDir
			= String.format("%s/%s",Environment.getExternalStorageDirectory().getPath() , folder );
		File file = new File(TargetRootDir);
		// ディレクトリ初期作成
		if (!file.exists()) {
			if (file.mkdir() == false) {
				Log.i("lightbox", "ディレクトリを作成できませんでした");
				return path;
			}
		}

		// 画像保存パス
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd_HHmmss");
		path = String.format("%s/%s.%s", TargetRootDir,sf.format(cal.getTime()),ext);

		Log.i("lightbox",path);

		return path;

	}
}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
AndroidManifest.xml ( No.2 )
日時: 2016/11/08 14:10
名前: lightbox
カメラを使用するのでデバイスの向きを portrait で固定しています
拡張子:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="lightbox.sep.fire1"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA"/>

</manifest>
このアーティクルの参照用URLをクリップボードにコピー メンテナンス