Reputation: 1
I have wrote this code with Android Studio Koala in java:
Library import:
implementation libs.zxing.android.embedded implementation libs.zxing.android.integration implementation libs.core
AndroidManifest:
`
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Scanner5"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
tools:replace="screenOrientation"
android:theme="@style/Theme.Scanner5">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>`
MainActivity.java
package com.example.scanner5;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Avvio della scansione
startBarcodeScan();
}
// Metodo per avviare la scansione
private void startBarcodeScan() {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan a barcode"); // Testo visualizzato durante la scansione
integrator.setCameraId(0); // Usa la fotocamera posteriore (1 per la fotocamera frontale)
integrator.setOrientationLocked(false); // Disabilita il blocco dell'orientamento
//integrator.setScanningRectangle(450, 450);//size
integrator.setBeepEnabled(true); // Abilita il suono alla scansione
//integrator.setBarcodeImageEnabled(true);
integrator.initiateScan(); // Avvia la scansione
}
// Gestione del risultato della scansione
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
// Puoi gestire il contenuto del barcode qui
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/button_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan Barcode" />
</LinearLayout>
Scanning only works in landscape mode. the APP only works in horizontal reading, the barcode reader does not turn with the vertical screen. From the examples found it should be automatic. How can I do?
https://github.com/journeyapps/zxing-android-embedded https://github.com/bitsoex/android-zxing-embedded
Upvotes: 0
Views: 64