How to ZXING Barcode Scanner not full screen only half screen
Asked Answered
O

6

4

I want create application Scan Barcode using ZXING Barcode Scanner

Like Blackberry Messenger

enter image description here

This is my code "MainActivity.java"

package com.example.ridwan.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import info.vividcode.android.zxing.CaptureActivity;
import info.vividcode.android.zxing.CaptureActivityIntents;

public class MainActivity extends AppCompatActivity  {

    private TextView tvScanResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent captureIntent = new Intent(MainActivity.this, CaptureActivity.class);
        CaptureActivityIntents.setPromptMessage(captureIntent, "Barcode scanning...");
        startActivityForResult(captureIntent, 0);

        tvScanResult = (TextView) findViewById(R.id.tv_scanresult);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                String value = data.getStringExtra("SCAN_RESULT");
                tvScanResult.setText(value);
            } else if (resultCode == Activity.RESULT_CANCELED) {
                tvScanResult.setText("Scanning Gagal, mohon coba lagi.");
            }
        } else {

        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}

Then this is my "activity_main.xml"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ridwan.myapplication.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_marginTop="50dp"
        android:id="@+id/tv_scanresult_title"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result Scan : " />

    <TextView
        android:layout_below="@id/tv_scanresult_title"
        android:id="@+id/tv_scanresult"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:textColor="#ff1493"
        android:layout_height="wrap_content"
        android:text="_" />

</RelativeLayout>

Can you give me solution ? i want to barcode in fragment.

Ovariotomy answered 30/12, 2016 at 7:30 Comment(0)
V
1

I have achieved the same effect/UI you are looking for by using ZXing Android Embedded. Very straightforward to implement - and it also includes a torch functionality.

Volturno answered 19/7, 2017 at 10:33 Comment(0)
S
1

Step 1:

Add This Libray in Gradle in Dependancy

implementation 'com.google.zxing:core:3.2.1'
implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'

Step 2:

BarcodeActivity.java

    public class BarcodeActivity extends AppCompatActivity {


       private EditText editTextProductId;
       private Button buttonGenerate, buttonScan;
       private ImageView imageViewResult;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_barcode);
          initView();
       }

       private void initView() {
           editTextProductId = findViewById(R.id.editTextProductId);
           imageViewResult = findViewById(R.id.imageViewResult);
           buttonGenerate = findViewById(R.id.buttonGenerate);

           buttonGenerate.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   buttonGenerate_onClick(view);
               }
           });
           buttonScan = findViewById(R.id.buttonScan);
           buttonScan.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   buttonScan_onClick(view);
               }
           });
       }

       private void buttonGenerate_onClick(View view) {
           try {
               String productId = editTextProductId.getText().toString();
               Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
               hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
               Writer codeWriter;
               codeWriter = new Code128Writer();
               BitMatrix byteMatrix = codeWriter.encode(productId, BarcodeFormat.CODE_128,400, 200, hintMap);
               int width = byteMatrix.getWidth();
               int height = byteMatrix.getHeight();
               Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
               for (int i = 0; i < width; i++) {
                   for (int j = 0; j < height; j++) {
                       bitmap.setPixel(i, j, byteMatrix.get(i, j) ? Color.BLACK : Color.WHITE);
                   }
               }
               imageViewResult.setImageBitmap(bitmap);
           } catch (Exception e) {
               Toast.makeText(getApplicationContext(), e.getMessage(), 
               Toast.LENGTH_LONG).show();
           }
       }

       private void buttonScan_onClick(View view) {
           IntentIntegrator intentIntegrator = new IntentIntegrator(this);
   intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
           intentIntegrator.setCameraId(0);
           intentIntegrator.initiateScan();
       }

       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
           if (intentResult != null) {
               String productId = intentResult.getContents();
               Toast.makeText(getApplicationContext(), productId, Toast.LENGTH_LONG).show();
           }
       }

    }

Step 3:

activity_barcode.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:ignore="HardcodedText">

<EditText
    android:id="@+id/editTextProductId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Product Id"
    android:inputType="textPersonName" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/buttonGenerate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Generate Barcode" />

    <Button
        android:id="@+id/buttonScan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Scan Barcode" />
</LinearLayout>

<ImageView
    android:id="@+id/imageViewResult"
    android:layout_width="match_parent"
    android:layout_height="335dp" />


</LinearLayout>
Sixty answered 7/11, 2019 at 5:55 Comment(0)
B
0

Please Add this code in MainActivity

Add This Libray in Gradle in Dependancy

    compile 'com.journeyapps:zxing-android-embedded:3.3.0@aar'
compile 'me.dm7.barcodescanner:zxing:1.9'

Add jar zbar.jar

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

ZXingScannerView mScannerView;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        QCscanner = (Button) findViewById(R.id.QCscanner);



mScannerView = new ZXingScannerView(this);
    QCscanner.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                /*Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                startActivityForResult(intent, 0);*/
                mScannerView = new ZXingScannerView(MainActivity.this);   // Programmatically initialize the scanner view<br />
                setContentView(mScannerView);
                mScannerView.setResultHandler(MainActivity.this); // Register ourselves as a handler for scan results.<br />
                mScannerView.startCamera();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

}

 @Override
public void handleResult(Result result) {
    Log.e("", result.getText()); // Prints scan results<br />
    Log.e("", result.getBarcodeFormat().toString());

    Toast.makeText(MainActivity.this, "" + result.getText() + "\n" + result.getBarcodeFormat().toString(), Toast.LENGTH_SHORT).show();

}

}

Bambara answered 30/12, 2016 at 7:45 Comment(2)
This my code, barcode already running, but i want not full screen scan barcode, i want scan barcode only half screenOvariotomy
use FrameLayout decalre height and width and in layout call your barcode xmlBambara
E
0

ZXING library allows you to launch an intent(activity) to scan barcodes. If you wants to make changes in that you have to make changes in CaptureActivity of ZXING lib.

Also, now since Google has included scanning feature in its playservices you can use Vision api for scanning in a fragment without integration of any third party library. https://github.com/googlesamples/android-vision/tree/master/visionSamples

Enzymolysis answered 30/12, 2016 at 9:4 Comment(0)
C
0

Please use https://github.com/journeyapps/zxing-android-embedded

Just include Scanner view and remove scan paddings by adding: app:zxing_framing_rect_width="200dp" app:zxing_framing_rect_height="200dp" attributes.

<com.journeyapps.barcodescanner.DecoratedBarcodeView
    android:id="@+id/zxing_barcode_scanner"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp"
    app:zxing_framing_rect_width="200dp"
    app:zxing_framing_rect_height="200dp"
    app:zxing_preview_scaling_strategy="fitXY"
    app:zxing_use_texture_view="false"
    />
Cranial answered 13/11, 2018 at 15:55 Comment(0)
D
0

try this. (It very simplified example):

First step. Add dependency in build.gradle(module app):

From version 4.x, only Android SDK 24+ is supported by default, and androidx is required.

implementation 'com.journeyapps:zxing-android-embedded:4.2.0'
implementation 'androidx.appcompat:appcompat:1.0.2'

For Android SDK versions < 24, you can downgrade zxing:core to 3.3.0 or earlier for Android 14+ support:

implementation 'com.google.zxing:core:3.3.0'

Second step. Paste DecoratedBarcodeView view in you layout (activity_main.xml in this example):

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <com.journeyapps.barcodescanner.DecoratedBarcodeView
        android:id="@+id/barcode_scanner"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">
    </com.journeyapps.barcodescanner.DecoratedBarcodeView>

    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/barcode_scanner" />
    </androidx.constraintlayout.widget.ConstraintLayout>

Third step. Add this code to MainActivity:

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    val formats: Collection<BarcodeFormat> = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39)
    binding.barcodeScanner.decoderFactory = DefaultDecoderFactory(formats)
    binding.barcodeScanner.initializeFromIntent(Intent())
    binding.barcodeScanner.decodeContinuous(object : BarcodeCallback {
        override fun barcodeResult(result: BarcodeResult?) {
            binding.resultTextView.text = result?.result?.text
        }
    })
}

override fun onResume() {
    super.onResume()
    binding.barcodeScanner.resume()
}

override fun onPause() {
    super.onPause()
    binding.barcodeScanner.pause()
}
}

Important! Implements onResume/onPause methods for activate DecoratedBarcodeView view. And don't forget grant Camera permissions for you app!

enter image description here

In this example i use this library: https://github.com/journeyapps/zxing-android-embedded

Decadent answered 5/9, 2021 at 19:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.