问题
I want create application Scan Barcode using ZXING Barcode Scanner
Like Blackberry Messenger
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.
回答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.
回答2:
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();
}
}
回答3:
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
回答4:
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"
/>
回答5:
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>
来源:https://stackoverflow.com/questions/41392426/how-to-zxing-barcode-scanner-not-full-screen-only-half-screen