newbie questions in android.

Hi, i am from Argentina and new in Android. In the school i see C-C++, but Android/Java is very different to me.  . From already thank you very much and sorry for my bad english. In first place, i am doing a simple QR Scanner Code for learn the basics of the language. I have a mess in my head and i want to solved this:

1)-  I use the Google Mobile Vision library, this include a class called Barcode and someone called BarcodeDetector.

The example code i am using is :

BarcodeDetector detector = new BarcodeDetector.Builder(context)
     .setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE)
     .build();

In first place, why this library have the class BarcodeDetector.Builder and BarcodeDetector, which is the difference between both? 

In second place, why the parameters passed to .setBarcodeFormats arent a object ? Barcode is a class, and Barcode.DATA_MATRIX as parameter, what is this?

2)- In other code i saw that it is common that some methods have as parameters:

(new SurfaceHolder.Callback(){

…}; 

//This is and a simple example, i need to learn about this parameters what start with (new… )

what is the name of this ? and where i read about this to learn? 

Let me start by pointing out these are confusions about Java vs. C/C++, not really Android-specific. Some recent introduction to Java materials might be helpful to study.

Something to keep in mind when using libraries like these, the underlying implementation is likely to be in C or C++, and the Java API is merely a thin layer over the C/C++ library. So sometimes the parameters or functions are declared the way they are because they need to conform to C/C++ conventions.

In first place, why this library have the class BarcodeDetector.Builder and BarcodeDetector, which is the difference between both? 

What you want to do is to study some “introduction to design patterns” material. This situation is a perfect example of the “Factory” pattern.

A BarcodeDetector is the class that will do the work. It recognises barcodes, etc. A BarcodeDetector instance is configured to recognise only specific barcode types or groups. The BarcodeDetector class most likely holds a list of specific barcode detectors which are executed against an image to see if it is a barcode.

The Builder is the factory which creates (“builds”) a BarcodeDetector instance. The Builder’s purpose is to create the detector, and by my hypothesis, connect the requested underlying detectors.

In second place, why the parameters passed to .setBarcodeFormats arent a object ? Barcode is a class, and Barcode.DATA_MATRIX as parameter, what is this?

This is a Java syntax difference from C/C++. In C/C++ you’d typically define the bitmasks like so:

#define AZTEC       0x01
#define CODE_128 0x02
#define CODE_39   0x04

etc.

The closest Java equivalent is to define these as constants within the Barcode class, E.g.:

public class Barcode
{
    public int AZTEC = 0x01;
    public int CODE_128 = 0x02;
    public int DATA_MATRIX = 0x20;
...
}

So to reference DATA_MATRIX, you write “Barcode.DATA_MATRIX”. Its merely an integer constant, this style is purely syntactic differences.

(new SurfaceHolder.Callback(){

…}; 

//This is and a simple example, i need to learn about this parameters what start with (new… )

what is the name of this ? and where i read about this to learn? 

I’m assuming you are talking about something like this:

holder.addCallback(new SurfaceHolder.Callback() {
    @Override
    public void surfaceCreated(SurfaceHolder h) {
    }
    @Override
    public void surfaceChanged(SurfaceHolder h, int format, int width, int height) {
      setSize(width, height);
      if (!ViewCompat.isInLayout(mSurfaceView)) {
        dispatchSurfaceChanged();
      }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder h) {
      setSize(0, 0);
    }
  });

The parameter that was passed to addCallback is called an “anonymous class”. This is a convenience way to declare a simple class “on the fly” without the programmer having to create a .java file to contain the class, give it a name, etc.  If you google “java anonymous class” you can find some references, such as this.

1 Like