Files included in the program:
AndroidManifest.xml
<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.CAMERA" />
build.gradle (Module :app)
def camerax_version = "1.2.2"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"
implementation 'com.google.mlkit:barcode-scanning:17.2.0'
implementation 'com.intuit.sdp:sdp-android:1.0.6'
implementation 'com.intuit.ssp:ssp-android:1.0.6'
implementation 'androidx.camera:camera-mlkit-vision:1.2.0-beta02'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.camera.view.PreviewView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/previewView"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.simpy.myapplication;
import static androidx.camera.view.CameraController.COORDINATE_SYSTEM_VIEW_REFERENCED;
import static androidx.camera.view.CameraController.IMAGE_ANALYSIS;
import static androidx.camera.view.CameraController.IMAGE_CAPTURE;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.CameraSelector;
import androidx.camera.mlkit.vision.MlKitAnalyzer;
import androidx.camera.view.LifecycleCameraController;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.google.mlkit.vision.barcode.BarcodeScanner;
import com.google.mlkit.vision.barcode.BarcodeScannerOptions;
import com.google.mlkit.vision.barcode.BarcodeScanning;
import com.google.mlkit.vision.barcode.common.Barcode;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MainActivity extends AppCompatActivity {
private PreviewView previewView;
private LifecycleCameraController cameraController;
private BarcodeScanner barcodeScanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
checkPermissions();
previewView = findViewById(R.id.previewView);
previewView.setScaleType(PreviewView.ScaleType.FIT_CENTER);
cameraController = new LifecycleCameraController(this);
// create BarcodeScanner object
BarcodeScannerOptions options = new BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_ALL_FORMATS)
.build();
barcodeScanner = BarcodeScanning.getClient(options);
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
cameraController.setCameraSelector(cameraSelector);
cameraController.setEnabledUseCases(IMAGE_CAPTURE | IMAGE_ANALYSIS);
cameraController.setImageAnalysisAnalyzer(ContextCompat.getMainExecutor(this),
new MlKitAnalyzer(List.of(barcodeScanner), COORDINATE_SYSTEM_VIEW_REFERENCED,
ContextCompat.getMainExecutor(this), result -> {
List<Barcode> barcodes = result.getValue(barcodeScanner);
if (barcodes.size() > 0) {
Toast.makeText(this, "Barcode: " + barcodes.get(0).getRawValue(), Toast.LENGTH_SHORT).show();
}
}));
previewView.setController(cameraController);
cameraController.bindToLifecycle(this);
}
public static final int PERMISSION_CAMERA = 1;
public void checkPermissions() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}
, PERMISSION_CAMERA);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
switch (requestCode) {
case PERMISSION_CAMERA:
Toast.makeText(this, "Camera access granted", Toast.LENGTH_SHORT)
.show();
break;
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
cameraController.unbind();
barcodeScanner.close();
}
}
Comments
Post a Comment