content: package com.amitshekhar.tflite;
  
  import android.graphics.Bitmap;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.text.method.ScrollingMovementMethod;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import com.wonderkiln.camerakit.CameraKitError;
  import com.wonderkiln.camerakit.CameraKitEvent;
  import com.wonderkiln.camerakit.CameraKitEventListener;
  import com.wonderkiln.camerakit.CameraKitImage;
  import com.wonderkiln.camerakit.CameraKitVideo;
  import com.wonderkiln.camerakit.CameraView;
  
  import java.util.List;
  import java.util.concurrent.Executor;
  import java.util.concurrent.Executors;
  
  public class MainActivity extends AppCompatActivity {
  
      private static final String MODEL_PATH = "mobilenet_quant_v1_224.tflite";
      private static final boolean QUANT = true;
      private static final String LABEL_PATH = "labels.txt";
      private static final int INPUT_SIZE = 224;
  
      private Classifier classifier;
  
      private Executor executor = Executors.newSingleThreadExecutor();
      private TextView textViewResult;
      private Button btnDetectObject, btnToggleCamera;
      private ImageView imageViewResult;
      private CameraView cameraView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          cameraView = findViewById(R.id.cameraView);
          imageViewResult = findViewById(R.id.imageViewResult);
          textViewResult = findViewById(R.id.textViewResult);
          textViewResult.setMovementMethod(new ScrollingMovementMethod());
  
          btnToggleCamera = findViewById(R.id.btnToggleCamera);
          btnDetectObject = findViewById(R.id.btnDetectObject);
  
          cameraView.addCameraKitListener(new CameraKitEventListener() {
              @Override
              public void onEvent(CameraKitEvent cameraKitEvent) {
  
              }
  
              @Override
              public void onError(CameraKitError cameraKitError) {
  
              }
  
              @Override
              public void onImage(CameraKitImage cameraKitImage) {
  
                  Bitmap bitmap = cameraKitImage.getBitmap();
  
                  bitmap = Bitmap.createScaledBitmap(bitmap, INPUT_SIZE, INPUT_SIZE, false);
  
                  imageViewResult.setImageBitmap(bitmap);
  
                  final List<Classifier.Recognition> results = classifier.recognizeImage(bitmap);
  
                  textViewResult.setText(results.toString());
  
              }
  
              @Override
              public void onVideo(CameraKitVideo cameraKitVideo) {
  
              }
          });
  
          btnToggleCamera.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  cameraView.toggleFacing();
              }
          });
  
          btnDetectObject.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  cameraView.captureImage();
              }
          });
  
          initTensorFlowAndLoadModel();
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          cameraView.start();
      }
  
      @Override
      protected void onPause() {
          cameraView.stop();
          super.onPause();
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          executor.execute(new Runnable() {
              @Override
              public void run() {
                  classifier.close();
              }
          });
      }
  
      private void initTensorFlowAndLoadModel() {
          executor.execute(new Runnable() {
              @Override
              public void run() {
                  try {
                      classifier = TensorFlowImageClassifier.create(
                              getAssets(),
                              MODEL_PATH,
                              LABEL_PATH,
                              INPUT_SIZE,
                              QUANT);
                      makeButtonVisible();
                  } catch (final Exception e) {
                      throw new RuntimeException("Error initializing TensorFlow!", e);
                  }
              }
          });
      }
  
      private void makeButtonVisible() {
          runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  btnDetectObject.setVisibility(View.VISIBLE);
              }
          });
      }
  }
  