How to ZXING Barcode Scanner not full screen only half screen

后端 未结 5 752
礼貌的吻别
礼貌的吻别 2020-12-02 02:14

I want create application Scan Barcode using ZXING Barcode Scanner

Like Blackberry Messenger

This is my code \"MainActivity.java\"

相关标签:
5条回答
  • 2020-12-02 02:55

    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

    0 讨论(0)
  • 2020-12-02 03:00

    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>
    
    0 讨论(0)
  • 2020-12-02 03:02

    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();
    
    }
    

    }

    0 讨论(0)
  • 2020-12-02 03:16

    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.

    0 讨论(0)
  • 2020-12-02 03:17

    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"
        />
    
    0 讨论(0)
提交回复
热议问题