Skip to content
This repository has been archived by the owner on Sep 27, 2019. It is now read-only.

Commit

Permalink
Auto-update
Browse files Browse the repository at this point in the history
  • Loading branch information
google-automerger committed Feb 16, 2018
1 parent e3435a5 commit 2757794
Show file tree
Hide file tree
Showing 53 changed files with 1,280 additions and 651 deletions.
42 changes: 9 additions & 33 deletions Application/build.gradle
Original file line number Diff line number Diff line change
@@ -1,49 +1,26 @@

buildscript {
repositories {
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}

apply plugin: 'com.android.application'

repositories {
jcenter()
maven {
url 'https://maven.google.com'
}
google()
}

dependencies {
compile "com.android.support:support-v4:26.1.0"
compile "com.android.support:support-v13:26.1.0"
compile "com.android.support:cardview-v7:26.1.0"
compile "com.android.support:appcompat-v7:26.1.0"
compile 'com.android.support:support-v4:24.0.0'
compile 'com.android.support:design:24.0.0'
compile "com.android.support:support-v4:27.0.2"
compile "com.android.support:support-v13:27.0.2"
compile "com.android.support:cardview-v7:27.0.2"
compile "com.android.support:appcompat-v7:27.0.2"
compile 'com.android.support:design:27.0.2'
}

// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
List<String> dirs = ['main']

android {

compileSdkVersion 26

buildToolsVersion "26.0.1"
compileSdkVersion 27

defaultConfig {
minSdkVersion 15
targetSdkVersion 26
targetSdkVersion 27
}

compileOptions {
Expand All @@ -62,5 +39,4 @@ android {
androidTest.java.srcDirs = ['tests/src']

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@

package com.example.android.basicpermissions;

import com.example.android.basicpermissions.camera.CameraPreviewActivity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.example.android.basicpermissions.camera.CameraPreviewActivity;

/**
* Launcher Activity that demonstrates the use of runtime permissions for Android M.
* This Activity requests permissions to access the camera
Expand Down Expand Up @@ -66,8 +67,7 @@ protected void onCreate(Bundle savedInstanceState) {
mLayout = findViewById(R.id.main_layout);

// Register a listener for the 'Show Camera Preview' button.
Button b = (Button) findViewById(R.id.button_open_camera);
b.setOnClickListener(new View.OnClickListener() {
findViewById(R.id.button_open_camera).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showCameraPreview();
Expand All @@ -76,20 +76,20 @@ public void onClick(View view) {
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
// BEGIN_INCLUDE(onRequestPermissionsResult)
if (requestCode == PERMISSION_REQUEST_CAMERA) {
// Request for camera permission.
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission has been granted. Start camera preview Activity.
Snackbar.make(mLayout, "Camera permission was granted. Starting preview.",
Snackbar.make(mLayout, R.string.camera_permission_granted,
Snackbar.LENGTH_SHORT)
.show();
startCamera();
} else {
// Permission request was denied.
Snackbar.make(mLayout, "Camera permission request was denied.",
Snackbar.make(mLayout, R.string.camera_permission_denied,
Snackbar.LENGTH_SHORT)
.show();
}
Expand All @@ -104,7 +104,7 @@ private void showCameraPreview() {
== PackageManager.PERMISSION_GRANTED) {
// Permission is already available, start camera preview
Snackbar.make(mLayout,
"Camera permission is available. Starting preview.",
R.string.camera_permission_available,
Snackbar.LENGTH_SHORT).show();
startCamera();
} else {
Expand All @@ -125,9 +125,9 @@ private void requestCameraPermission() {
Manifest.permission.CAMERA)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// Display a SnackBar with a button to request the missing permission.
Snackbar.make(mLayout, "Camera access is required to display the camera preview.",
Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() {
// Display a SnackBar with cda button to request the missing permission.
Snackbar.make(mLayout, R.string.camera_access_required,
Snackbar.LENGTH_INDEFINITE).setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
// Request the permission
Expand All @@ -138,12 +138,10 @@ public void onClick(View view) {
}).show();

} else {
Snackbar.make(mLayout,
"Permission is not available. Requesting camera permission.",
Snackbar.LENGTH_SHORT).show();
Snackbar.make(mLayout, R.string.camera_unavailable, Snackbar.LENGTH_SHORT).show();
// Request the permission. The result will be received in onRequestPermissionResult().
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
PERMISSION_REQUEST_CAMERA);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
Expand All @@ -27,7 +28,7 @@

/**
* Camera preview that displays a {@link Camera}.
*
* <p>
* Handles basic lifecycle methods to display and stop the preview.
* <p>
* Implementation is based directly on the documentation at
Expand All @@ -36,14 +37,19 @@
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private static final String TAG = "CameraPreview";

private SurfaceHolder mHolder;
private Camera mCamera;
private Camera.CameraInfo mCameraInfo;
private int mDisplayOrientation;

public CameraPreview(Context context, Camera camera, Camera.CameraInfo cameraInfo,
int displayOrientation) {
super(context);
public CameraPreview(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, null, null, 0);
}

public CameraPreview(Context context, AttributeSet attrs, int defStyleAttr,
Camera camera, Camera.CameraInfo cameraInfo, int displayOrientation) {
super(context, attrs, defStyleAttr);

// Do not initialise if no camera has been set
if (camera == null || cameraInfo == null) {
Expand All @@ -59,6 +65,41 @@ public CameraPreview(Context context, Camera camera, Camera.CameraInfo cameraInf
mHolder.addCallback(this);
}

/**
* Calculate the correct orientation for a {@link Camera} preview that is displayed on screen.
* <p>
* Implementation is based on the sample code provided in
* {@link Camera#setDisplayOrientation(int)}.
*/
public static int calculatePreviewOrientation(Camera.CameraInfo info, int rotation) {
int degrees = 0;

switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}

int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}

return result;
}

public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
Expand Down Expand Up @@ -104,39 +145,4 @@ public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}

/**
* Calculate the correct orientation for a {@link Camera} preview that is displayed on screen.
*
* Implementation is based on the sample code provided in
* {@link Camera#setDisplayOrientation(int)}.
*/
public static int calculatePreviewOrientation(Camera.CameraInfo info, int rotation) {
int degrees = 0;

switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}

int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

package com.example.android.basicpermissions.camera;

import com.example.android.basicpermissions.R;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.example.android.basicpermissions.R;

/**
* Displays a {@link CameraPreview} of the first {@link Camera}.
* An error message is displayed if the Camera is not available.
Expand All @@ -36,15 +37,12 @@
* http://developer.android.com/guide/topics/media/camera.html
*/
public class CameraPreviewActivity extends Activity {

private static final String TAG = "CameraPreview";

private static final String TAG = "CameraPreviewActivity";
/**
* Id of the camera to access. 0 is the first camera.
*/
private static final int CAMERA_ID = 0;

private CameraPreview mPreview;
private Camera mCamera;

@Override
Expand All @@ -56,22 +54,21 @@ protected void onCreate(Bundle savedInstanceState) {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(CAMERA_ID, cameraInfo);

if (mCamera == null || cameraInfo == null) {
if (mCamera == null) {
// Camera is not available, display error message
Toast.makeText(this, "Camera is not available.", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_camera_unavailable);
} else {

setContentView(R.layout.activity_camera);

// Get the rotation of the screen to adjust the preview image accordingly.
final int displayRotation = getWindowManager().getDefaultDisplay()
.getRotation();
int displayRotation = getWindowManager().getDefaultDisplay().getRotation();

// Create the Preview view and set it as the content of this Activity.
mPreview = new CameraPreview(this, mCamera, cameraInfo, displayRotation);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
CameraPreview cameraPreview = new CameraPreview(this, null,
0, mCamera, cameraInfo, displayRotation);
FrameLayout preview = findViewById(R.id.camera_preview);
preview.addView(cameraPreview);
}
}

Expand All @@ -82,22 +79,26 @@ public void onPause() {
releaseCamera();
}

/** A safe way to get an instance of the Camera object. */
/**
* A safe way to get an instance of the Camera object.
*/
private Camera getCameraInstance(int cameraId) {
Camera c = null;
try {
c = Camera.open(cameraId); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
Toast.makeText(this, "Camera " + cameraId + " is not available: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
Log.e(TAG, "Camera " + cameraId + " is not available: " + e.getMessage());
}
return c; // returns null if camera is unavailable
}

/**
* Release the camera for other applications.
*/
private void releaseCamera() {
if (mCamera != null) {
mCamera.release(); // release the camera for other applications
mCamera.release();
mCamera = null;
}
}
Expand Down
11 changes: 5 additions & 6 deletions Application/src/main/res/layout/activity_camera.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2015 The Android Open Source Project
Copyright 2017 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.
Expand All @@ -15,8 +15,7 @@
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
Loading

0 comments on commit 2757794

Please sign in to comment.