Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
compileSdkVersion 26
buildToolsVersion '26.0.2'

defaultConfig {
applicationId "com.example.android.barcode_scanner"
minSdkVersion 21
targetSdkVersion 24
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
Expand All @@ -22,6 +22,6 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.google.android.gms:play-services:7.8+'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.google.android.gms:play-services:11.0.2'
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.example.android.barcode_scanner;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
Expand All @@ -17,26 +23,35 @@

public class MainActivity extends Activity {

private CameraSource cameraSource;
private SurfaceView cameraView;
private int CAMERA_REQUEST_CODE=334;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SurfaceView cameraView = (SurfaceView) findViewById(R.id.camera_view);
final TextView barcodeInfo = (TextView) findViewById(R.id.code_info);
cameraView = findViewById(R.id.camera_view);
final TextView barcodeInfo = findViewById(R.id.code_info);
BarcodeDetector barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.QR_CODE)
.build();

final CameraSource cameraSource = new CameraSource
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
return;
}
}
cameraSource.start(cameraView.getHolder());

} catch (IOException ie) {
Expand Down Expand Up @@ -75,4 +90,27 @@ public void run() {
}
});
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == CAMERA_REQUEST_CODE) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
if (cameraSource != null) {
try {
cameraSource.start(cameraView.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}

} else {
Toast.makeText(this, "Need camera permission", Toast.LENGTH_LONG).show();
}

}
}
}
29 changes: 13 additions & 16 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp">
android:layout_width="match_parent"
android:layout_height="match_parent">



<SurfaceView
android:layout_width="640px"
android:layout_height="480px"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="@+id/camera_view"
/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/code_info"
android:layout_toRightOf="@+id/camera_view"
android:textSize="20sp"
android:layout_marginLeft="16dp"
android:text="Nothing to read."
android:layout_alignParentTop="true"
/>

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="@string/str_nothing_to_read"
android:textSize="@dimen/text_size_medium" />
</RelativeLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="text_size_small">14sp</dimen>
<dimen name="text_size_medium">16sp</dimen>
<dimen name="text_size_large">18sp</dimen>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">Barcode_Scanner</string>
<string name="str_nothing_to_read">Nothing to read.</string>
</resources>
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:3.0.0-beta7'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -15,6 +16,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Aug 21 00:53:27 IST 2016
#Sat Oct 14 10:43:32 IST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip