1 |
torben |
2527 |
package dk.thoerup.android.barcodesample;
|
2 |
|
|
|
3 |
|
|
import android.content.Intent;
|
4 |
|
|
import android.support.v7.app.ActionBarActivity;
|
5 |
|
|
import android.os.Bundle;
|
6 |
|
|
import android.view.Menu;
|
7 |
|
|
import android.view.MenuItem;
|
8 |
|
|
import android.view.View;
|
9 |
|
|
import android.widget.Button;
|
10 |
|
|
import android.widget.TextView;
|
11 |
|
|
import android.widget.Toast;
|
12 |
|
|
|
13 |
|
|
import com.google.zxing.integration.android.IntentIntegrator;
|
14 |
|
|
import com.google.zxing.integration.android.IntentResult;
|
15 |
|
|
|
16 |
|
|
/*
|
17 |
|
|
http://blog.dihaw.com/integrating-zxing-in-your-android-app-as-standalone-scanner/
|
18 |
|
|
|
19 |
|
|
http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
http://stackoverflow.com/questions/27851512/how-to-integrate-zxing-library-to-android-studio-for-barcode-scanning
|
23 |
|
|
|
24 |
|
|
*/
|
25 |
|
|
|
26 |
|
|
public class MainActivity extends ActionBarActivity {
|
27 |
|
|
|
28 |
|
|
private Button scanBtn;
|
29 |
|
|
private TextView formatTxt, contentTxt;
|
30 |
|
|
|
31 |
|
|
@Override
|
32 |
|
|
protected void onCreate(Bundle savedInstanceState) {
|
33 |
|
|
super.onCreate(savedInstanceState);
|
34 |
|
|
setContentView(R.layout.activity_main);
|
35 |
|
|
|
36 |
|
|
scanBtn = (Button) findViewById(R.id.button);
|
37 |
|
|
formatTxt = (TextView)findViewById(R.id.scan_format);
|
38 |
|
|
contentTxt = (TextView)findViewById(R.id.scan_content);
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
scanBtn.setOnClickListener(new View.OnClickListener() {
|
44 |
|
|
|
45 |
|
|
@Override
|
46 |
|
|
public void onClick(View v) {
|
47 |
|
|
/*Intent intent = new Intent("com.google.zxing.client.android.SCAN");
|
48 |
|
|
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
|
49 |
|
|
startActivityForResult(intent, 0);*/
|
50 |
|
|
|
51 |
|
|
IntentIntegrator scanIntegrator = new IntentIntegrator(MainActivity.this);
|
52 |
|
|
scanIntegrator.initiateScan();
|
53 |
|
|
}
|
54 |
|
|
});
|
55 |
|
|
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
59 |
|
|
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
|
60 |
|
|
|
61 |
|
|
if (scanningResult != null) {
|
62 |
|
|
String scanContent = scanningResult.getContents();
|
63 |
|
|
String scanFormat = scanningResult.getFormatName();
|
64 |
|
|
|
65 |
|
|
formatTxt.setText("FORMAT: " + scanFormat);
|
66 |
|
|
contentTxt.setText("CONTENT: " + scanContent);
|
67 |
|
|
}
|
68 |
|
|
else{
|
69 |
|
|
Toast toast = Toast.makeText(getApplicationContext(), "No scan data received!", Toast.LENGTH_SHORT);
|
70 |
|
|
toast.show();
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
@Override
|
75 |
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
76 |
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
77 |
|
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
78 |
|
|
return true;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
@Override
|
82 |
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
83 |
|
|
// Handle action bar item clicks here. The action bar will
|
84 |
|
|
// automatically handle clicks on the Home/Up button, so long
|
85 |
|
|
// as you specify a parent activity in AndroidManifest.xml.
|
86 |
|
|
int id = item.getItemId();
|
87 |
|
|
|
88 |
|
|
//noinspection SimplifiableIfStatement
|
89 |
|
|
if (id == R.id.action_settings) {
|
90 |
|
|
return true;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
return super.onOptionsItemSelected(item);
|
94 |
|
|
}
|
95 |
|
|
}
|