如何ZXING条码扫描器不是全屏只有半屏

时间:2016-12-30 07:30:58

标签: java android xml zxing barcode-scanner

我想要创建应用程序 使用ZXING条形码扫描仪扫描条形码

喜欢Blackberry Messenger

enter image description here

这是我的代码" 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);
    }


}

然后这是我的" 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>
你能给我解决方案吗? 我想在片段中条形码。

5 个答案:

答案 0 :(得分:2)

我使用ZXing Android Embedded获得了您正在寻找的相同效果/用户界面。非常简单的实施 - 它还包括火炬功能。

答案 1 :(得分:0)

请在MainActivity中添加此代码

在依赖性Gradle中添加此Libray

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

添加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();

}

}

答案 2 :(得分:0)

ZXING库允许您启动意图(活动)来扫描条形码。如果您想进行更改,则必须在ZXING lib的CaptureActivity中进行更改。

此外,由于Google已在其播放服务中包含扫描功能,因此您可以使用Vision api扫描片段,而无需集成任何第三方库。 https://github.com/googlesamples/android-vision/tree/master/visionSamples

答案 3 :(得分:0)

请使用 https://github.com/journeyapps/zxing-android-embedded

只需包括“扫描程序”视图并通过添加以下内容删除扫描填充:        应用:zxing_framing_rect_width =“ 200dp”         应用:zxing_framing_rect_height =“ 200dp”    属性。

<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"
    />

答案 4 :(得分:0)

第1步:

  

在依赖关系中将此Libray添加到Gradle中

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

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

    }

第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>