Java tutorial
package com.scandit.simplesample; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import java.io.IOException; import org.json.JSONException; import java.io.InputStream; import java.net.URL; import java.io.BufferedReader; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.io.Reader; import android.app.Activity; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import android.widget.Toast; import com.scandit.barcodepicker.BarcodePicker; import com.scandit.barcodepicker.OnScanListener; import com.scandit.barcodepicker.ScanSession; import com.scandit.barcodepicker.ScanSettings; import com.scandit.barcodepicker.ScanditLicense; import com.scandit.recognition.Barcode; import android.util.Log; import java.util.Locale; /** * Simple demo application illustrating the use of the Scandit BarcodeScanner SDK. */ /* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied * See the License for the specific language governing premissions and * limitations under the License. */ public class SimpleSampleActivity extends Activity implements OnScanListener { // The main object for recognizing a displaying barcodes. private BarcodePicker mBarcodePicker; // Enter your Scandit SDK App key here. // Your Scandit SDK App key is available via your Scandit SDK web account. public static final String sScanditSdkAppKey = "+EXaDpWbSTPV2BbvLSIPwM7Oi4mUz6mPrSBCvCAYocM"; Toast mToast = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ScanditLicense.setAppKey(sScanditSdkAppKey); // Initialize and start the bar code recognition. initializeAndStartBarcodeScanning(); } @Override protected void onPause() { // When the activity is in the background immediately stop the // scanning to save resources and free the camera. mBarcodePicker.stopScanning(); super.onPause(); } @Override protected void onResume() { // Once the activity is in the foreground again, restart scanning. mBarcodePicker.startScanning(); super.onResume(); } public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { Log.d("STATE", "1"); InputStream is = new URL(url).openStream(); Log.d("STATE", "2"); try { Log.d("STATE", "3"); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); Log.d("STATE", "4"); String jsonText = readAll(rd); Log.d("STATE", "5"); JSONObject json = new JSONObject(jsonText); Log.d("STATE", "6"); return json; } finally { Log.d("STATE", "7"); is.close(); } } public static String readAll(Reader r) throws IOException { StringBuilder s = new StringBuilder(); try { int i = r.read(); while (i != -1) { s.append((char) (i)); i = r.read(); } return s.toString(); } finally { } } /** * Initializes and starts the bar code scanning. */ public void initializeAndStartBarcodeScanning() { // Switch to full screen. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); // The scanning behavior of the barcode picker is configured through scan // settings. We start with empty scan settings and enable a very generous // set of symbologies. In your own apps, only enable the symbologies you // actually need. ScanSettings settings = ScanSettings.create(); settings.setSymbologyEnabled(Barcode.SYMBOLOGY_EAN13, true); settings.setSymbologyEnabled(Barcode.SYMBOLOGY_UPCA, true); settings.setSymbologyEnabled(Barcode.SYMBOLOGY_EAN8, true); settings.setSymbologyEnabled(Barcode.SYMBOLOGY_UPCE, true); settings.setSymbologyEnabled(Barcode.SYMBOLOGY_QR, true); settings.setSymbologyEnabled(Barcode.SYMBOLOGY_DATA_MATRIX, true); settings.setSymbologyEnabled(Barcode.SYMBOLOGY_CODE39, true); settings.setSymbologyEnabled(Barcode.SYMBOLOGY_CODE128, true); settings.setSymbologyEnabled(Barcode.SYMBOLOGY_INTERLEAVED_2_OF_5, true); settings.setCameraFacingPreference(ScanSettings.CAMERA_FACING_BACK); // Some Android 2.3+ devices do not support rotated camera feeds. On these devices, the // barcode picker emulates portrait mode by rotating the scan UI. boolean emulatePortraitMode = !BarcodePicker.canRunPortraitPicker(); if (emulatePortraitMode) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } BarcodePicker picker = new BarcodePicker(this, settings); setContentView(picker); mBarcodePicker = picker; // Register listener, in order to be notified about relevant events // (e.g. a successfully scanned bar code). mBarcodePicker.setOnScanListener(this); } /** * Called when a barcode has been decoded successfully. */ public void didScan(ScanSession session) { String message = ""; for (Barcode code : session.getNewlyRecognizedCodes()) { String data = code.getData(); // truncate code to certain length String cleanData = data; if (data.length() > 30) { cleanData = data.substring(0, 25) + "[...]"; } if (message.length() > 0) { message += "\n\n\n"; } message += cleanData; message += "\n\n(" + code.getSymbologyName().toUpperCase(Locale.US) + ")"; try { JSONObject result = readJsonFromUrl( "http://api.upcdatabase.org/json/5e01140d7ff62fcc5c2c5f56cb1516d1/" + cleanData); System.out.print(result.toString()); } catch (JSONException e) { } catch (IOException e) { } } if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(this, message, Toast.LENGTH_LONG); mToast.show(); } @Override public void onBackPressed() { mBarcodePicker.stopScanning(); finish(); } }