Skip to content

Commit

Permalink
Basic camera preview on Camera1
Browse files Browse the repository at this point in the history
Change-Id: Iace32d2a230016229b60084bfe060c4ec7bca8b5
  • Loading branch information
yaraki committed Mar 29, 2016
1 parent a6f9e36 commit e03c259
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0-alpha3'
classpath 'com.android.tools.build:gradle:2.1.0-alpha4'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 3 additions & 1 deletion demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
<manifest package="com.google.android.cameraview.demo"
xmlns:android="http://schemas.android.com/apk/res/android">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/Theme.Demo">

<activity android:name=".MainActivity">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,34 @@

package com.google.android.cameraview.demo;

import com.google.android.cameraview.CameraView;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private CameraView mCameraView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCameraView = (CameraView) findViewById(R.id.camera);
}

@Override
protected void onResume() {
super.onResume();
mCameraView.onResume();
mCameraView.startPreview();
}

@Override
protected void onPause() {
mCameraView.stopPreview();
mCameraView.onPause();
super.onPause();
}

}
2 changes: 1 addition & 1 deletion demo/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="Theme.Demo" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
Expand Down
5 changes: 5 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/base'
main.java.srcDirs += 'src/main/camera1'
main.java.srcDirs += 'src/main/camera2'
}
}

dependencies {
Expand Down
23 changes: 23 additions & 0 deletions library/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<manifest package="com.google.android.cameraview"
xmlns:android="http://schemas.android.com/apk/res/android">

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

<application>
<activity android:name=".CameraViewActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.app.Activity;
import android.os.Bundle;

public class CameraViewActivity extends Activity {

private CameraView mCameraView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_view);
mCameraView = (CameraView) findViewById(R.id.camera);
}

@Override
protected void onResume() {
super.onResume();
mCameraView.onResume();
mCameraView.startPreview();
}

@Override
protected void onPause() {
mCameraView.stopPreview();
mCameraView.onPause();
super.onPause();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,83 @@

package com.google.android.cameraview;

import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.annotation.UiThreadTest;
import android.support.test.rule.UiThreadTestRule;
import android.graphics.Bitmap;
import android.os.SystemClock;
import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.ViewAssertion;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.TextureView;
import android.view.View;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static junit.framework.Assert.assertFalse;

@RunWith(AndroidJUnit4.class)
public class CameraViewTest {

public final UiThreadTestRule rule;
@Rule
public final ActivityTestRule<CameraViewActivity> rule;

public CameraViewTest() {
rule = new UiThreadTestRule();
rule = new ActivityTestRule<>(CameraViewActivity.class);
}

@Test
@UiThreadTest
public void testConstructor() {
Context context = InstrumentationRegistry.getTargetContext();
CameraView cameraView = new CameraView(context);
assertThat(cameraView.getChildCount(), is(0));
public void testSetup() {
onView(withId(R.id.camera))
.check(matches(isDisplayed()));
onView(withId(R.id.texture_view))
.check(matches(isDisplayed()));
}

@Test
public void preview_isShowing() {
onView(withId(R.id.texture_view))
.check(matches(isDisplayed()))
// TODO: Replace below with an IdlingCallback when state listener is implemented
.perform(new WaitAction())
.check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
TextureView textureView = (TextureView) view;
Bitmap bitmap = textureView.getBitmap();
int topLeft = bitmap.getPixel(0, 0);
int topRight = bitmap.getPixel(0, bitmap.getHeight() - 1);
int bottomLeft = bitmap.getPixel(bitmap.getWidth() - 1, 0);
assertFalse(topLeft == topRight && topRight == bottomLeft);
}
});
}

// A temporary workaround
private static class WaitAction implements ViewAction {

@Override
public Matcher<View> getConstraints() {
return isDisplayed();
}

@Override
public String getDescription() {
return "aaa";
}

@Override
public void perform(UiController uiController, View view) {
SystemClock.sleep(1000);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.view.TextureView;

abstract class CameraViewImpl {

abstract TextureView.SurfaceTextureListener getSurfaceTextureListener();

abstract void onResume();

abstract void onPause();

abstract void startPreview();

abstract void stopPreview();

}
Loading

0 comments on commit e03c259

Please sign in to comment.