Slide 1

Slide 1 text

Camera + 回転 + TextureView 2016.05.25 @関モバ

Slide 2

Slide 2 text

Who? Twitter: @masanori_msl vaguely: http://mslgt.hatenablog.com/ SearchWakayamaToilet: https://play.google.com/store/apps/details? id=jp.searchwakayamatoilet Name: Masui Masanori

Slide 3

Slide 3 text

Camera2  Android5.0(Lollipop)から追加された、Camera 機能を扱うためのAPI。  IntentからCameraアプリを呼び出すより扱いが 難しいが、UIや機能などを独自でカスタマイズ できる。  プレビュー画面の表示にはTextureViewを使用す る。

Slide 4

Slide 4 text

Camera2  端末が回転した時に、Landscapeモードと Portraitモードを切り替える場合、自分で TextureViewを回転させる必要がある。  TextureViewの回転にはMatrixを使い、  TextureViewの角度を指定すると同時にサイズを 調整する。

Slide 5

Slide 5 text

Sample andCameraTest 参考 googlesamples - android-Camera2Basic https://github.com/masanori840816/andCameraTest https://github.com/googlesamples/android-Camera2Basic

Slide 6

Slide 6 text

回転 Camera2Activity.java private void configureTransform(int viewWidth, int viewHeight){ // 画面の回転に合わせてTextureViewの向き、サイズを変更する. if (previewTextureView == null || previewSize == null){ return; } runOnUiThread(() ->{ RectF rctView = new RectF(0, 0, viewWidth, viewHeight); RectF rctPreview = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth()); float centerX = rctView.centerX(); float centerY = rctView.centerY(); Matrix matrix = new Matrix(); int deviceRotation = getWindowManager().getDefaultDisplay().getRotation(); if(deviceRotation == Surface.ROTATION_90 || deviceRotation == Surface.ROTATION_270){ // Landscapeモードの場合のみサイズ調整. rctPreview.offset(centerX - rctPreview.centerX(), centerY – rctPreview.centerY()); matrix.setRectToRect(rctView, rctPreview, Matrix.ScaleToFit.FILL);

Slide 7

Slide 7 text

回転 // 縦または横の画面一杯に表示するためのScale値を取得. float scale = Math.max( (float) viewHeight / previewSize.getHeight() , (float) viewWidth / previewSize.getWidth() ); matrix.postScale(scale, scale, centerX, centerY); // ROTATION_90: 270度回転、ROTATION_270: 90度回転. matrix.postRotate((90 * (deviceRotation + 2)) % 360, centerX, centerY); } else{ // ROTATION_0: 0度回転、ROTATION_180: 180度回転. matrix.postRotate(90 * deviceRotation, centerX, centerY); } previewTextureView.setTransform(matrix); }); }

Slide 8

Slide 8 text

 起動直後を除き、端末を回転させたときはこの処 理が2回呼ばれる。  引数として渡されるサイズは、1回目はディスプ レイサイズ、2回目はプレビューとして指定した サイズが渡される。  (Sampleでは) ActionBarを表示するとLandscape モードとPortraitモードのTextureViewサイズが 揃わないため非表示にする。 回転

Slide 9

Slide 9 text

Android Nでの変更は?

Slide 10

Slide 10 text

 Camera2 APIは特に変更なさそう。  明示的にサポートしていなくてもMultiWindow で動作可能なため、何らかの対応が必要。 (少なくともクラッシュしないように) Android Nでの変更は?

Slide 11

Slide 11 text

MultiWindow対応

Slide 12

Slide 12 text

MultiWindow対応 準備 AndroidManifest.xmlのapplicationタグに「resizeableActivity を 」 追加する。 ※この要素が無いとMultiWindowで開くときに 「App may not work with split-screen.」 とToast が表示される。

Slide 13

Slide 13 text

MultiWindow対応  MultiWindowのOn / Off切替時、Windowサイズ の変更時はActivityが破棄される。  Windowのサイズによって表示上の画面の向きが 変わってしまうため、以下の値 (端末・画面の向 き) が異なる場合がある。 // 例: 端末が縦向きならWindowサイズによらず0°となる. int displayOrientation = getWindowManager().getDefaultDisplay().getRotation(); // 例: 端末が縦向きでもWindowサイズによって // Landscape、Portraitの値が変わる. Configuration config = getResources().getConfiguration(); int configOrientation = config.orientation;

Slide 14

Slide 14 text

MultiWindow対応 Windowが横長の場合 Windowが縦長の場合

Slide 15

Slide 15 text

 Camera2のAPIの機能(フィルターやFlashlightな ど)を試してみたい。  TextureViewの表示位置やサイズを自由に指定で きるようにしたい  ( 今回は画面上端 / 左端からフルサイズで表示し ただけだったため)。  MultiWindowのFreedomWindowモードの対 応。 課題

Slide 16

Slide 16 text

参考 android - How to detect screen rotation through 180 degrees from landscape to landscape orientation? - Stack Overflow android.hardware.camera2 - Android Developers googlesamples - android-Camera2Basic https://developer.android.com/reference/android/hardware/ca mera2/package-summary.html http://stackoverflow.com/questions/9909037/how-to-detect- screen-rotation-through-180-degrees-from-landscape-to- landscape-or https://github.com/googlesamples/android-Camera2Basic

Slide 17

Slide 17 text

参考 android-MultiWindowPlayground - googlesamples - GitHub Android N で注意すること その3 (Multi Window その1) - Qiita Multi-Window Support - Android Developers Android N で注意すること その4 (Multi Window その2) - Qiita https://developer.android.com/preview/features/multi- window.html https://github.com/googlesamples/android- MultiWindowPlayground http://qiita.com/hidenorly/items/456b4dd725875b776d9a http://qiita.com/hidenorly/items/72a659438126a8457715#_refer ence-7ccad824470f0f9c04fb

Slide 18

Slide 18 text

Credit Google Noto Fonts  Main Page:  License: Material icons  Main Page:  License:  Main Page:  License: googlesamples - android-Camera2Basic https://www.google.com/get/noto/ SIL Open Font License (OFL) https://design.google.com/icons/ https://github.com/googlesamples/android- Camera2Basic CC-BY License Apache License 2.0

Slide 19

Slide 19 text

Credit Copyright 2014 The Android Open Source Project, Inc. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Slide 20

Slide 20 text

Thank you.