I created a basic barcode scanner app with Java and Android Studio (on my Mac paradoxically). In this app, I simply uploaded (manually) some photos of barcodes to Android Studio in the . /app/src/main/res/drawable folder from my desktop and then I am sending one of these photos each time to Google Mobile Vision Barcode API . I receive from the API the data represented from the barcode and I simply print the data on the screen of the Android emulator. The MainActivity.java script of this app is the following:
package *********************; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.SparseArray; import android.widget.ImageView; import android.widget.TextView; import com.google.android.gms.vision.Frame; import com.google.android.gms.vision.barcode.Barcode; import com.google.android.gms.vision.barcode.BarcodeDetector; public class MainActivity extends AppCompatActivity < @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ImageView myImageView = (ImageView) findViewById(R.id.imgview); Bitmap myBitmap = BitmapFactory.decodeResource( getApplicationContext().getResources(), R.drawable.image); myImageView.setImageBitmap(myBitmap); TextView txtView = (TextView) findViewById(R.id.txtContent); BarcodeDetector detector = new BarcodeDetector.Builder(getApplicationContext()) .setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE | Barcode.CODE_128) .build(); if(!detector.isOperational())< txtView.setText("Could not set up the detector!"); return; >Frame frame = new Frame.Builder().setBitmap(myBitmap).build(); SparseArray barcodes = detector.detect(frame); Barcode thisCode = barcodes.valueAt(0); txtView.setText(thisCode.rawValue); >
Now I want to use the retrieved data from the Barcode API, send them to (a server and to) a database and retrieve some details stored in this database about the product that has this barcode. What is the most appropriate way to make my Java app to communicate with a database? Personally, I am pretty confident in creating a MySQL database and sending back and forth data with PHP. However, I am not really sure how to connect a script written in Java (which scans barcodes) to a database.