简体   繁体   中英

Difficulty intergrating zxing QR code scanner into my Android app

Have started to develop my first "real" original app for android using android studio, so I'm still extremely new to it, though i have experience in java. My app involves using a QR code scanner and using what it outputs(the specific use will be with bitcoin addresses).

I'm having real problems integrating the QR code scanner, honestly i don't really care right know whether it's called using an intent or I'm using the libraries internally, i just really want to do it the easiest way. My problem is i really don't understand how to import the packages and what not, and i have a really hard time finding detailed explanations on how to do this online.

The ideal way would be to press a button, open the scanner, when you then have scanned a code it will go to another activity where you can maybe view the output and then have choices to what you want to do with it.

here is my main activity if you can use it for anything:

package com.JunkerDevBlog.bitcoinaddresssender;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@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 scan(View view) {

    Intent intent = new Intent(this, ScanActivity.class);

    startActivity(intent);

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

Also i'm using android studio, help is GREATLY appreciated.

If you setup Zxing correctly, just need the code below...

IntentIntegrator.initiateScan(BarcodeActivity.this, R.layout.capture, R.id.viewfinder_view, R.id.preview_view, true); // Call the Scanner Intenet

And here you get the return from barcode reader

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case IntentIntegrator.REQUEST_CODE:
            IntentResult scanResult = IntentIntegrator.parseActivityResult(
                    requestCode, resultCode, data);
            if (scanResult == null) {
                return;
            }
            final String result = scanResult.getContents(); // Your result

            if (result != null) {
                System.out.print("Your result is: " + result);
            }
            break;
        default:
        }
    }

LayoutCapture.xml

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <SurfaceView android:id="@+id/preview_view"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_centerInParent="true" />

    <jim.h.common.android.zxinglib.view.ViewfinderView
        android:id="@+id/viewfinder_view" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:background="#00000000" />

    <TextView android:id="@+id/status_view" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal"
        android:background="#00000000" 
        android:textColor="#ffffffff" android:textSize="14sp" />
</FrameLayout>

A personal opinion, I found the integration of ZXing over complicated and instead choose to integrated ZBar library.

You can get it from here: http://sourceforge.net/projects/zbar/files/AndroidSDK/ (download the 0.2 zip it includes the lib and an example project.)

Ok First things first !

** firing an intent to pull up zxing application is not a wise choice (say if the user does not have a network connection and doesnt have the zxing app, he cant use ur app, so I strongly recommend u use the zxing library)

  1. Android Studio is not completely ready as of now. Its still in a beta version. So use eclipse and ADT plugin

  2. Follow this tutorial to setup zxing. My friend wrote this one after we worked on a project involving zxing.

http://www.androiddevelopersolution.com/2012/06/integrating-zxing-qr-code-scanner-into.html

  1. Always keep in mind that, ZXING IS NOT FRAGMENT FRIENDLY ! On a project I am in right now, our app's heart is zxing and its working great.

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM