0

I am new to Android. I am trying to read the barcode using zbar. I am testing with DushyanthMaguluru/ZBarScanner/ZBarScannerDemo.

The problem is the application is opening and when i click scan button it simply opens the camera and the red line to detect the barcode is not coming. I dont know where i went wrong.

and my MAinactivity.Java code is

package com.example.zbartest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;
import android.widget.TextView;
import com.dm.zbar.android.scanner.ZBarConstants;
import com.dm.zbar.android.scanner.ZBarScannerActivity;
import net.sourceforge.zbar.Symbol;
public class MainActivity extends Activity {

private static final int ZBAR_SCANNER_REQUEST = 0;
private static final int ZBAR_QR_SCANNER_REQUEST = 1;

private TextView formatTxt, contentTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    formatTxt = (TextView)findViewById(R.id.scan_format);
    contentTxt = (TextView)findViewById(R.id.scan_content);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void launchScanner(View v) {
    if (isCameraAvailable()) {
        Intent intent = new Intent(this, ZBarScannerActivity.class);
        startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
    } else {
        Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
    }
}

public void launchQRScanner(View v) {
    if (isCameraAvailable()) {
        Intent intent = new Intent(this, ZBarScannerActivity.class);
        intent.putExtra(ZBarConstants.SCAN_MODES, new int[]{Symbol.QRCODE});
        startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
    } else {
        Toast.makeText(this, "Rear Facing Camera Unavailable", Toast.LENGTH_SHORT).show();
    }
}

public boolean isCameraAvailable() {
    PackageManager pm = getPackageManager();
    return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case ZBAR_SCANNER_REQUEST:
        case ZBAR_QR_SCANNER_REQUEST:
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, "Scan Result = " + data.getStringExtra(ZBarConstants.SCAN_RESULT), Toast.LENGTH_SHORT).show();                         
                String scanFormat = data.getStringExtra("SCAN_RESULT");
                String scanContent = data.getStringExtra("SCAN_RESULT_FORMAT");
                formatTxt.setText("FORMAT: " + scanFormat);
                contentTxt.setText("CONTENT: " + scanContent);
            } else if(resultCode == RESULT_CANCELED && data != null) {
                String error = data.getStringExtra(ZBarConstants.ERROR_INFO);
                if(!TextUtils.isEmpty(error)) {
                    Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
                }
            }
            break;
    }
}
}

I have added the ZbarScannerActivity,CameraPreview,ZbarConstatnts java files in the src folder of my project and added the ZbarScannerlibrary files into the libs folder. I just copied the three ZbarScannerActivity,CameraPreview,ZbarConstatnts files from the sample and pasted into my project.

Kindly help

Sarulatha
  • 31
  • 3

1 Answers1

0

Maybe you need to clean and rebuild the project.

and try it again

user3277530
  • 351
  • 6
  • 14