Skip to content

Commit

Permalink
Add app:focusMode
Browse files Browse the repository at this point in the history
Change-Id: I91eaa845c6afd80c99fad9ec1979254dec2ef9ab
  • Loading branch information
yaraki committed May 30, 2016
1 parent 7ddd7f9 commit 052ebe6
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 9 deletions.
4 changes: 3 additions & 1 deletion demo/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
-->
<FrameLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -22,6 +23,7 @@
android:id="@+id/camera"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"/>
android:adjustViewBounds="true"
app:focusMode="continuousPicture"/>

</FrameLayout>
4 changes: 3 additions & 1 deletion demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
-->
<FrameLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -22,6 +23,7 @@
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
android:adjustViewBounds="true"
app:focusMode="continuousPicture"/>

</FrameLayout>
2 changes: 2 additions & 0 deletions library/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<application>
<activity android:name=".CameraViewActivity"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.android.cameraview;

import com.google.android.cameraview.test.R;

import android.app.Activity;
import android.os.Bundle;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.android.cameraview;

import com.google.android.cameraview.test.R;

import org.hamcrest.Matcher;
import org.hamcrest.core.IsAnything;
import org.junit.After;
Expand Down Expand Up @@ -202,6 +204,21 @@ public void check(View view, NoMatchingViewException noViewFoundException) {
});
}

@Test
public void testFocusMode() {
onView(withId(R.id.camera))
.check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
CameraView cameraView = (CameraView) view;
assertThat(cameraView.getFocusMode(), is(CameraView.FOCUS_MODE_OFF));
cameraView.setFocusMode(CameraView.FOCUS_MODE_CONTINUOUS_PICTURE);
assertThat(cameraView.getFocusMode(),
is(CameraView.FOCUS_MODE_CONTINUOUS_PICTURE));
}
});
}

/**
* Wait for a camera to open.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public CameraViewImpl(Callback callback) {

abstract int getDisplayOrientation();

abstract void setFocusMode(int focusMode);

abstract int getFocusMode();

interface Callback {

void onCameraOpened();
Expand Down
29 changes: 29 additions & 0 deletions library/src/main/base/com/google/android/cameraview/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed 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.
*/

package com.google.android.cameraview;


interface Constants {

int FOCUS_MODE_OFF = 0;
int FOCUS_MODE_AUTO = 1;
int FOCUS_MODE_MACRO = 2;
int FOCUS_MODE_CONTINUOUS_PICTURE = 3;
int FOCUS_MODE_CONTINUOUS_VIDEO = 4;
int FOCUS_MODE_EDOF = 5;

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Camera1 extends CameraViewImpl {

private boolean mShowingPreview;

private int mFocusMode;

private static class PreviewInfo {
SurfaceTexture surface;
int width;
Expand All @@ -69,22 +71,22 @@ void configure(SurfaceTexture s, int w, int h) {
private final TextureView.SurfaceTextureListener mSurfaceTextureListener
= new TextureView.SurfaceTextureListener() {

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
private void reconfigurePreview(SurfaceTexture surface, int width, int height) {
mPreviewInfo.configure(surface, width, height);
if (mCamera != null) {
setUpPreview();
adjustPreviewSize();
}
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
reconfigurePreview(surface, width, height);
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
mPreviewInfo.configure(surface, width, height);
if (mCamera != null) {
setUpPreview();
adjustPreviewSize();
}
reconfigurePreview(surface, width, height);
}

@Override
Expand Down Expand Up @@ -172,6 +174,22 @@ int getDisplayOrientation() {
return mDisplayOrientation;
}

@Override
void setFocusMode(int focusMode) {
if (mFocusMode != focusMode) {
mFocusMode = focusMode;
if (mCamera != null) {
mCameraParameters.setFocusMode(Camera1Constants.convertFocusMode(focusMode));
mCamera.setParameters(mCameraParameters);
}
}
}

@Override
int getFocusMode() {
return mFocusMode;
}

/**
* This rewrites {@link #mCameraId} and {@link #mCameraInfo}.
*/
Expand Down Expand Up @@ -232,6 +250,7 @@ private void adjustPreviewSize() {
mCamera.stopPreview();
}
mCameraParameters.setPreviewSize(size.getWidth(), size.getHeight());
mCameraParameters.setFocusMode(Camera1Constants.convertFocusMode(mFocusMode));
mCamera.setParameters(mCameraParameters);
if (mShowingPreview) {
mCamera.startPreview();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed 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.
*/

package com.google.android.cameraview;


import android.hardware.Camera;
import android.support.v4.util.SparseArrayCompat;

@SuppressWarnings("deprecation")
class Camera1Constants {

private static SparseArrayCompat<String> sFocusModesMap;

static String convertFocusMode(int focusMode) {
if (sFocusModesMap == null) {
sFocusModesMap = new SparseArrayCompat<>();
sFocusModesMap.put(Constants.FOCUS_MODE_OFF, Camera.Parameters.FOCUS_MODE_FIXED);
sFocusModesMap.put(Constants.FOCUS_MODE_AUTO, Camera.Parameters.FOCUS_MODE_AUTO);
sFocusModesMap.put(Constants.FOCUS_MODE_MACRO, Camera.Parameters.FOCUS_MODE_MACRO);
sFocusModesMap.put(Constants.FOCUS_MODE_CONTINUOUS_PICTURE,
Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
sFocusModesMap.put(Constants.FOCUS_MODE_CONTINUOUS_VIDEO,
Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
sFocusModesMap.put(Constants.FOCUS_MODE_EDOF, Camera.Parameters.FOCUS_MODE_EDOF);
}
return sFocusModesMap.get(focusMode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,32 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.TextureView;
import android.widget.FrameLayout;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;

public class CameraView extends FrameLayout {

public static final int FOCUS_MODE_OFF = Constants.FOCUS_MODE_OFF;
public static final int FOCUS_MODE_AUTO = Constants.FOCUS_MODE_AUTO;
public static final int FOCUS_MODE_MACRO = Constants.FOCUS_MODE_MACRO;
public static final int FOCUS_MODE_CONTINUOUS_PICTURE = Constants.FOCUS_MODE_CONTINUOUS_PICTURE;
public static final int FOCUS_MODE_CONTINUOUS_VIDEO = Constants.FOCUS_MODE_CONTINUOUS_VIDEO;
public static final int FOCUS_MODE_EDOF = Constants.FOCUS_MODE_EDOF;

@IntDef({FOCUS_MODE_OFF, FOCUS_MODE_AUTO, FOCUS_MODE_MACRO, FOCUS_MODE_CONTINUOUS_PICTURE,
FOCUS_MODE_CONTINUOUS_VIDEO, FOCUS_MODE_EDOF})
@Retention(RetentionPolicy.SOURCE)
public @interface FocusMode {
}

private final CameraViewImpl mImpl;

private final CallbackBridge mCallbacks;
Expand Down Expand Up @@ -63,6 +79,8 @@ public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraView, defStyleAttr,
R.style.Widget_CameraView);
mAdjustViewBounds = a.getBoolean(R.styleable.CameraView_android_adjustViewBounds, false);
//noinspection WrongConstant
setFocusMode(a.getInt(R.styleable.CameraView_focusMode, FOCUS_MODE_OFF));
a.recycle();
}

Expand Down Expand Up @@ -210,6 +228,29 @@ public AspectRatio getAspectRatio() {
return mImpl.getAspectRatio();
}

/**
* Sets the focus mode.
*
* @param focusMode The focus mode. Must be one of {@link #FOCUS_MODE_OFF},
* {@link #FOCUS_MODE_AUTO}, {@link #FOCUS_MODE_MACRO},
* {@link #FOCUS_MODE_CONTINUOUS_PICTURE},
* {@link #FOCUS_MODE_CONTINUOUS_VIDEO}, and {@link #FOCUS_MODE_EDOF}.
*/
public void setFocusMode(@FocusMode int focusMode) {
mImpl.setFocusMode(focusMode);
}

/**
* Gets the current focus mode.
*
* @return The current focus mode.
*/
@FocusMode
public int getFocusMode() {
//noinspection WrongConstant
return mImpl.getFocusMode();
}

private class CallbackBridge implements CameraViewImpl.Callback {

private final ArrayList<Callback> mCallbacks = new ArrayList<>();
Expand Down
8 changes: 8 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
<resources>
<declare-styleable name="CameraView">
<attr name="android:adjustViewBounds"/>
<attr name="focusMode" format="enum">
<enum name="off" value="0"/>
<enum name="auto" value="1"/>
<enum name="macro" value="2"/>
<enum name="continuousPicture" value="3"/>
<enum name="continuousVideo" value="4"/>
<enum name="edof" value="5"/>
</attr>
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions library/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<style name="Widget.CameraView" parent="android:Widget">
<item name="android:adjustViewBounds">false</item>
<item name="focusMode">off</item>
</style>

</resources>

0 comments on commit 052ebe6

Please sign in to comment.