1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.camera; 18 19 import android.graphics.Bitmap; 20 import android.graphics.Matrix; 21 import android.graphics.RectF; 22 import android.view.TextureView; 23 import android.view.View; 24 import android.view.ViewGroup; 25 26 import com.android.camera.debug.Log; 27 import com.android.camera.ui.CountDownView; 28 import com.android.camera.ui.PreviewOverlay; 29 import com.android.camera.ui.PreviewOverlay.OnZoomChangedListener; 30 import com.android.camera.ui.PreviewStatusListener; 31 import com.android.camera.ui.ProgressOverlay; 32 import com.android.camera.ui.focus.FocusRing; 33 import com.android.camera2.R; 34 35 /** 36 * Contains the UI for the CaptureModule. 37 */ 38 public class CaptureModuleUI implements PreviewStatusListener.PreviewAreaChangedListener { 39 40 public interface CaptureModuleUIListener { onZoomRatioChanged(float zoomRatio)41 public void onZoomRatioChanged(float zoomRatio); 42 } 43 44 private static final Log.Tag TAG = new Log.Tag("CaptureModuleUI"); 45 46 private final CameraActivity mActivity; 47 private final CaptureModuleUIListener mListener; 48 private final View mRootView; 49 50 private final PreviewOverlay mPreviewOverlay; 51 private final ProgressOverlay mProgressOverlay; 52 private final TextureView mPreviewView; 53 54 private final FocusRing mFocusRing; 55 private final CountDownView mCountdownView; 56 57 private int mPreviewAreaWidth; 58 private int mPreviewAreaHeight; 59 60 /** Maximum zoom; intialize to 1.0 (disabled) */ 61 private float mMaxZoom = 1f; 62 63 /** Set up listener to receive zoom changes from View and send to module. */ 64 private final OnZoomChangedListener mZoomChangedListener = new OnZoomChangedListener() { 65 @Override 66 public void onZoomValueChanged(float ratio) { 67 mListener.onZoomRatioChanged(ratio); 68 } 69 70 @Override 71 public void onZoomStart() { 72 } 73 74 @Override 75 public void onZoomEnd() { 76 } 77 }; 78 CaptureModuleUI(CameraActivity activity, View parent, CaptureModuleUIListener listener)79 public CaptureModuleUI(CameraActivity activity, View parent, CaptureModuleUIListener listener) { 80 mActivity = activity; 81 mListener = listener; 82 mRootView = parent; 83 84 ViewGroup moduleRoot = (ViewGroup) mRootView.findViewById(R.id.module_layout); 85 mActivity.getLayoutInflater().inflate(R.layout.capture_module, moduleRoot, true); 86 87 mPreviewView = (TextureView) mRootView.findViewById(R.id.preview_content); 88 89 mPreviewOverlay = (PreviewOverlay) mRootView.findViewById(R.id.preview_overlay); 90 mProgressOverlay = (ProgressOverlay) mRootView.findViewById(R.id.progress_overlay); 91 92 mFocusRing = (FocusRing) mRootView.findViewById(R.id.focus_ring); 93 mCountdownView = (CountDownView) mRootView.findViewById(R.id.count_down_view); 94 } 95 96 /** 97 * Getter for the width of the visible area of the preview. 98 */ getPreviewAreaWidth()99 public int getPreviewAreaWidth() { 100 return mPreviewAreaWidth; 101 } 102 103 /** 104 * Getter for the height of the visible area of the preview. 105 */ getPreviewAreaHeight()106 public int getPreviewAreaHeight() { 107 return mPreviewAreaHeight; 108 } 109 getPreviewTransform(Matrix m)110 public Matrix getPreviewTransform(Matrix m) { 111 return mPreviewView.getTransform(m); 112 } 113 getFocusRing()114 public FocusRing getFocusRing() { 115 return mFocusRing; 116 } 117 showDebugMessage(String message)118 public void showDebugMessage(String message) { 119 /* NoOp */ 120 } 121 122 /** 123 * Starts the countdown timer. 124 * 125 * @param sec seconds to countdown 126 */ startCountdown(int sec)127 public void startCountdown(int sec) { 128 mCountdownView.startCountDown(sec); 129 } 130 131 /** 132 * Sets a listener that gets notified when the countdown is finished. 133 */ setCountdownFinishedListener(CountDownView.OnCountDownStatusListener listener)134 public void setCountdownFinishedListener(CountDownView.OnCountDownStatusListener listener) { 135 mCountdownView.setCountDownStatusListener(listener); 136 } 137 138 /** 139 * Returns whether the countdown is on-going. 140 */ isCountingDown()141 public boolean isCountingDown() { 142 return mCountdownView.isCountingDown(); 143 } 144 145 /** 146 * Cancels the on-going countdown, if any. 147 */ cancelCountDown()148 public void cancelCountDown() { 149 mCountdownView.cancelCountDown(); 150 } 151 152 /** 153 * Sets the progress of the gcam picture taking. 154 * 155 * @param percent amount of process done in percent 0-100. 156 */ setPictureTakingProgress(int percent)157 public void setPictureTakingProgress(int percent) { 158 mProgressOverlay.setProgress(percent); 159 } 160 getBitMapFromPreview()161 public Bitmap getBitMapFromPreview() { 162 Matrix m = new Matrix(); 163 m = getPreviewTransform(m); 164 Bitmap src = mPreviewView.getBitmap(); 165 return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true); 166 } 167 168 /** 169 * Enables zoom UI, setting maximum zoom. 170 * Called from Module when camera is available. 171 * 172 * @param maxZoom maximum zoom value. 173 */ initializeZoom(float maxZoom)174 public void initializeZoom(float maxZoom) { 175 mMaxZoom = maxZoom; 176 mPreviewOverlay.setupZoom(mMaxZoom, 0, mZoomChangedListener); 177 } 178 179 @Override onPreviewAreaChanged(RectF previewArea)180 public void onPreviewAreaChanged(RectF previewArea) { 181 // TODO: mFaceView.onPreviewAreaChanged(previewArea); 182 mCountdownView.onPreviewAreaChanged(previewArea); 183 mProgressOverlay.setBounds(previewArea); 184 } 185 } 186