com.projectcs2103t.openglestest.OpenGLES20Activity.java Source code

Java tutorial

Introduction

Here is the source code for com.projectcs2103t.openglestest.OpenGLES20Activity.java

Source

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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 permissions and
 * limitations under the License.
 */
package com.projectcs2103t.openglestest;

import android.app.Activity;
import android.hardware.Camera;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import java.io.File;
import java.util.List;

public class OpenGLES20Activity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2 {
    private static final String TAG = "OpenGLES20Activity";

    private GLSurfaceView mGLView;
    private ImageView backgroundView;
    private CameraBridgeViewBase mOpenCvCameraView;
    private List<Camera.Size> mSupportedImageSizes;
    private CameraProjectionAdapter mCameraProjectionAdapter;
    private NativeLinker nl;

    private static final int MENU_GROUP_ID_SIZE = 2;
    private static final String STATE_IMAGE_SIZE_INDEX = "STATE_IMAGE_SIZE_INDEX";
    private int mImageSizeIndex = 11;
    private int mCameraIndex = 0;

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
            case LoaderCallbackInterface.SUCCESS:
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
                mOpenCvCameraView.enableFpsMeter();
                initNative();
                break;
            default:
                super.onManagerConnected(status);
                break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        if (savedInstanceState != null) {
            mImageSizeIndex = savedInstanceState.getInt(STATE_IMAGE_SIZE_INDEX, 0);
        }
        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity
        final FrameLayout layout = new FrameLayout(this);
        layout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        setContentView(layout);

        /*
        backgroundView = new ImageView(this);
        backgroundView.setImageResource(R.drawable.ic_launcher);
        backgroundView.setLayoutParams(new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT));
        layout.addView(backgroundView);*/

        mOpenCvCameraView = new JavaCameraView(this, mCameraIndex);
        mOpenCvCameraView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        layout.addView(mOpenCvCameraView);
        mOpenCvCameraView.setCvCameraViewListener(this);

        Camera camera = Camera.open();
        Camera.Parameters parameters = camera.getParameters();
        camera.release();
        mSupportedImageSizes = parameters.getSupportedPreviewSizes();
        Camera.Size size = mSupportedImageSizes.get(mImageSizeIndex);
        mOpenCvCameraView.setMaxFrameSize(size.width, size.height);
        mCameraProjectionAdapter = new CameraProjectionAdapter();
        mCameraProjectionAdapter.setCameraParameters(parameters, size);
        mCameraProjectionAdapter.setClipDistances(0.1f, 1000.0f);

        mGLView = new MyGLSurfaceView(this, mCameraProjectionAdapter);
        mGLView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        layout.addView(mGLView);
    }

    private void initNative() {
        String secStore = System.getenv("EXTERNAL_STORAGE");//internal sd
        System.out.println("external storage:" + secStore);
        String modelFile = secStore + "/virtual/model.ply";
        File f = new File(modelFile);
        System.out.println("file existence = " + f.exists());
        System.out.println("file readable = " + f.canRead());

        nl = new NativeLinker();
        nl.loadModel(modelFile);
        try {
            Mat patternImg = Utils.loadResource(OpenGLES20Activity.this, R.drawable.pattern1, 1);
            nl.loadPattern(patternImg.getNativeObjAddr());
            patternImg = Utils.loadResource(OpenGLES20Activity.this, R.drawable.pattern2, 1);
            nl.loadPattern(patternImg.getNativeObjAddr());
            patternImg = Utils.loadResource(OpenGLES20Activity.this, R.drawable.pattern3, 1);
            nl.loadPattern(patternImg.getNativeObjAddr());
        } catch (Exception e) {
        }
    }

    @Override
    protected void onPause() {
        if (mOpenCvCameraView != null) {
            mOpenCvCameraView.disableView();
        }
        super.onPause();
        // The following call pauses the rendering thread.
        // If your OpenGL application is memory intensive,
        // you should consider de-allocating objects that
        // consume significant memory here.
        mGLView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!OpenCVLoader.initDebug()) {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
        } else {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
        // The following call resumes a paused rendering thread.
        // If you de-allocated graphic objects for onPause()
        // this is a good place to re-allocate them.
        mGLView.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        int numSupportedImageSizes = mSupportedImageSizes.size();
        if (numSupportedImageSizes > 1) {
            SubMenu mCamSizes = menu.addSubMenu("CamSizes");
            for (int i = 0; i < numSupportedImageSizes; i++) {
                final Camera.Size size = mSupportedImageSizes.get(i);
                mCamSizes.add(MENU_GROUP_ID_SIZE, i, Menu.NONE, String.format("%dx%d", size.width, size.height));
            }
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        if (item.getGroupId() == MENU_GROUP_ID_SIZE) {
            mImageSizeIndex = item.getItemId();
            recreate();
        }
        return true;
    }

    @Override
    public void recreate() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            super.recreate();
        } else {
            finish();
            startActivity(getIntent());
        }
    }

    @Override
    public void onCameraViewStarted(int width, int height) {

    }

    @Override
    public void onCameraViewStopped() {

    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        savedInstanceState.putInt(STATE_IMAGE_SIZE_INDEX, mImageSizeIndex);
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        Mat rgba = inputFrame.rgba();
        float projection[] = mCameraProjectionAdapter.getProjectionGL();
        Mat CameraMat = mCameraProjectionAdapter.getCVCameraMat();
        Mat DistortionMat = mCameraProjectionAdapter.getCVDistortionMat();
        Mat ModelViewMat = new Mat(4, 4, CvType.CV_64FC1);
        int detected = nl.processFrame(rgba.getNativeObjAddr(), CameraMat.getNativeObjAddr(),
                DistortionMat.getNativeObjAddr(), ModelViewMat.getNativeObjAddr());
        float mGLModelView[] = null;
        if (detected == 1) {
            mGLModelView = new float[16];
            mGLModelView[0] = (float) ModelViewMat.get(0, 0)[0];
            mGLModelView[1] = (float) ModelViewMat.get(0, 1)[0];
            mGLModelView[2] = (float) ModelViewMat.get(0, 2)[0];
            mGLModelView[3] = (float) ModelViewMat.get(0, 3)[0];
            mGLModelView[4] = (float) ModelViewMat.get(1, 0)[0];
            mGLModelView[5] = (float) ModelViewMat.get(1, 1)[0];
            mGLModelView[6] = (float) ModelViewMat.get(1, 2)[0];
            mGLModelView[7] = (float) ModelViewMat.get(1, 3)[0];
            mGLModelView[8] = (float) ModelViewMat.get(2, 0)[0];
            mGLModelView[9] = (float) ModelViewMat.get(2, 1)[0];
            mGLModelView[10] = (float) ModelViewMat.get(2, 2)[0];
            mGLModelView[11] = (float) ModelViewMat.get(2, 3)[0];
            mGLModelView[12] = (float) ModelViewMat.get(3, 0)[0];
            mGLModelView[13] = (float) ModelViewMat.get(3, 1)[0];
            mGLModelView[14] = (float) ModelViewMat.get(3, 2)[0];
            mGLModelView[15] = (float) ModelViewMat.get(3, 3)[0];
            //showMatrices(rgba, ModelViewMat);
        }
        mCameraProjectionAdapter.setModelViewGL(mGLModelView);
        Imgproc.putText(rgba, mCameraProjectionAdapter.toString(), new Point(50, 50), Core.FONT_HERSHEY_PLAIN, 1.0,
                new Scalar(0, 255, 0));
        Imgproc.putText(rgba, mGLView.toString(), new Point(50, 75), Core.FONT_HERSHEY_PLAIN, 1.0,
                new Scalar(0, 255, 0));
        return rgba;
    }

    private void showMatrices(Mat rgba, Mat mat) {
        double m00 = mat.get(0, 0)[0];
        double m01 = mat.get(0, 1)[0];
        double m02 = mat.get(0, 2)[0];
        double m03 = mat.get(0, 3)[0];
        double m10 = mat.get(1, 0)[0];
        double m11 = mat.get(1, 1)[0];
        double m12 = mat.get(1, 2)[0];
        double m13 = mat.get(1, 3)[0];
        double m20 = mat.get(2, 0)[0];
        double m21 = mat.get(2, 1)[0];
        double m22 = mat.get(2, 2)[0];
        double m23 = mat.get(2, 3)[0];
        double m30 = mat.get(3, 0)[0];
        double m31 = mat.get(3, 1)[0];
        double m32 = mat.get(3, 2)[0];
        double m33 = mat.get(3, 3)[0];
        //String camMatStr = cameraMat.dump();
        String mRow0 = "|" + m00 + "," + m01 + "," + m02 + "," + m03 + "|";
        String mRow1 = "|" + m10 + "," + m11 + "," + m12 + "," + m13 + "|";
        String mRow2 = "|" + m20 + "," + m21 + "," + m22 + "," + m23 + "|";
        String mRow3 = "|" + m30 + "," + m31 + "," + m32 + "," + m33 + "|";
        Imgproc.putText(rgba, "Model-View-Mat:", new Point(50, 100), Core.FONT_HERSHEY_PLAIN, 1.0,
                new Scalar(0, 255, 0));
        Imgproc.putText(rgba, mRow0, new Point(50, 125), Core.FONT_HERSHEY_PLAIN, 1.0, new Scalar(0, 255, 0));
        Imgproc.putText(rgba, mRow1, new Point(50, 150), Core.FONT_HERSHEY_PLAIN, 1.0, new Scalar(0, 255, 0));
        Imgproc.putText(rgba, mRow2, new Point(50, 175), Core.FONT_HERSHEY_PLAIN, 1.0, new Scalar(0, 255, 0));
        Imgproc.putText(rgba, mRow3, new Point(50, 200), Core.FONT_HERSHEY_PLAIN, 1.0, new Scalar(0, 255, 0));

    }

}