2013-12-16 14:47:33 -04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 Miguel Angel Astor Romero
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2013-11-21 08:10:12 -04:30
|
|
|
package ve.ucv.ciens.ccg.nxtar;
|
|
|
|
|
|
2014-03-11 18:31:51 -04:30
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
|
2014-01-24 12:24:51 -04:30
|
|
|
import org.opencv.android.OpenCVLoader;
|
2014-03-12 16:34:52 -04:30
|
|
|
import org.opencv.android.Utils;
|
|
|
|
|
import org.opencv.core.Mat;
|
|
|
|
|
import org.opencv.imgproc.Imgproc;
|
2014-01-24 12:24:51 -04:30
|
|
|
|
2014-03-11 18:31:51 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtar.interfaces.CVProcessor;
|
2014-04-10 17:55:31 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtar.interfaces.OSFunctionalityProvider;
|
2014-04-30 13:05:36 -04:30
|
|
|
import ve.ucv.ciens.ccg.nxtar.utils.ProjectConstants;
|
2013-11-26 17:58:26 -04:30
|
|
|
import android.content.Context;
|
2014-01-24 12:24:51 -04:30
|
|
|
import android.content.pm.ActivityInfo;
|
2014-03-11 18:31:51 -04:30
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Bitmap.CompressFormat;
|
2014-03-12 16:34:52 -04:30
|
|
|
import android.graphics.BitmapFactory;
|
2013-11-26 17:58:26 -04:30
|
|
|
import android.net.wifi.WifiManager;
|
|
|
|
|
import android.net.wifi.WifiManager.MulticastLock;
|
2013-11-21 08:10:12 -04:30
|
|
|
import android.os.Bundle;
|
2013-11-28 09:01:45 -04:30
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.widget.Toast;
|
2013-11-21 08:10:12 -04:30
|
|
|
|
2013-11-27 08:11:37 -04:30
|
|
|
import com.badlogic.gdx.Gdx;
|
2013-11-21 08:10:12 -04:30
|
|
|
import com.badlogic.gdx.backends.android.AndroidApplication;
|
|
|
|
|
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
|
2014-01-24 12:24:51 -04:30
|
|
|
import com.badlogic.gdx.controllers.mappings.Ouya;
|
2013-11-21 08:10:12 -04:30
|
|
|
|
2014-04-10 17:55:31 -04:30
|
|
|
public class MainActivity extends AndroidApplication implements OSFunctionalityProvider, CVProcessor{
|
2013-11-26 17:58:26 -04:30
|
|
|
private static final String TAG = "NXTAR_ANDROID_MAIN";
|
|
|
|
|
private static final String CLASS_NAME = MainActivity.class.getSimpleName();
|
|
|
|
|
|
2014-04-10 17:55:31 -04:30
|
|
|
private static boolean ocvOn = false;
|
2014-04-30 13:05:36 -04:30
|
|
|
private static Mat cameraMatrix, distortionCoeffs;
|
2014-04-10 17:55:31 -04:30
|
|
|
|
2013-11-26 17:58:26 -04:30
|
|
|
private WifiManager wifiManager;
|
|
|
|
|
private MulticastLock multicastLock;
|
2013-11-28 09:01:45 -04:30
|
|
|
private Handler uiHandler;
|
|
|
|
|
private Context uiContext;
|
2014-03-11 18:31:51 -04:30
|
|
|
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
2013-11-26 17:58:26 -04:30
|
|
|
|
2014-04-10 17:55:31 -04:30
|
|
|
public native void getMarkerCodesAndLocations(long inMat, long outMat, int[] codes);
|
|
|
|
|
public native boolean findCalibrationPattern(long inMat, long outMat, float[] points);
|
|
|
|
|
|
2014-04-05 15:03:53 -04:30
|
|
|
static{
|
2014-04-10 17:55:31 -04:30
|
|
|
if(!OpenCVLoader.initDebug())
|
|
|
|
|
ocvOn = false;
|
|
|
|
|
|
2014-04-05 15:03:53 -04:30
|
|
|
try{
|
|
|
|
|
System.loadLibrary("cvproc");
|
|
|
|
|
ocvOn = true;
|
|
|
|
|
}catch(UnsatisfiedLinkError u){
|
2014-04-10 17:55:31 -04:30
|
|
|
ocvOn = false;
|
2014-04-05 15:03:53 -04:30
|
|
|
}
|
|
|
|
|
}
|
2014-03-11 18:31:51 -04:30
|
|
|
|
2013-11-21 08:10:12 -04:30
|
|
|
@Override
|
2014-02-10 15:25:36 -04:30
|
|
|
public void onCreate(Bundle savedInstanceState){
|
2013-11-21 08:10:12 -04:30
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
2014-04-30 13:05:36 -04:30
|
|
|
cameraMatrix = new Mat();
|
|
|
|
|
distortionCoeffs = new Mat();
|
|
|
|
|
|
2014-01-24 12:24:51 -04:30
|
|
|
if(!Ouya.runningOnOuya){
|
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
|
|
|
|
}else{
|
|
|
|
|
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-28 09:01:45 -04:30
|
|
|
uiHandler = new Handler();
|
|
|
|
|
uiContext = this;
|
2013-11-26 17:58:26 -04:30
|
|
|
wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
|
|
|
|
|
|
2013-11-21 08:10:12 -04:30
|
|
|
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
|
2014-02-06 18:57:39 -04:30
|
|
|
cfg.useGL20 = true;
|
2014-01-21 12:21:22 -04:30
|
|
|
cfg.useAccelerometer = false;
|
|
|
|
|
cfg.useCompass = false;
|
|
|
|
|
cfg.useWakelock = true;
|
2013-11-21 08:10:12 -04:30
|
|
|
|
2013-11-28 09:01:45 -04:30
|
|
|
initialize(new NxtARCore(this), cfg);
|
2013-11-26 17:58:26 -04:30
|
|
|
}
|
|
|
|
|
|
2014-04-10 17:55:31 -04:30
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
// OSFunctionalityProvider interface methods. //
|
|
|
|
|
////////////////////////////////////////////////
|
2013-11-28 09:01:45 -04:30
|
|
|
@Override
|
2014-02-10 15:25:36 -04:30
|
|
|
public void showShortToast(final String msg){
|
|
|
|
|
uiHandler.post(new Runnable(){
|
2013-11-28 09:01:45 -04:30
|
|
|
@Override
|
2014-02-10 15:25:36 -04:30
|
|
|
public void run(){
|
2013-11-28 09:01:45 -04:30
|
|
|
Toast.makeText(uiContext, msg, Toast.LENGTH_SHORT).show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-02-10 15:25:36 -04:30
|
|
|
public void showLongToast(final String msg){
|
|
|
|
|
uiHandler.post(new Runnable(){
|
2013-11-28 09:01:45 -04:30
|
|
|
@Override
|
2014-02-10 15:25:36 -04:30
|
|
|
public void run(){
|
2013-11-28 09:01:45 -04:30
|
|
|
Toast.makeText(uiContext, msg, Toast.LENGTH_LONG).show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-26 17:58:26 -04:30
|
|
|
@Override
|
|
|
|
|
public void enableMulticast(){
|
2014-01-24 12:24:51 -04:30
|
|
|
Gdx.app.log(TAG, CLASS_NAME + ".enableMulticast() :: Requesting multicast lock.");
|
2013-11-26 17:58:26 -04:30
|
|
|
multicastLock = wifiManager.createMulticastLock(TAG);
|
|
|
|
|
multicastLock.setReferenceCounted(true);
|
|
|
|
|
multicastLock.acquire();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-02-10 15:25:36 -04:30
|
|
|
public void disableMulticast(){
|
2014-01-24 12:24:51 -04:30
|
|
|
Gdx.app.log(TAG, CLASS_NAME + ".disableMulticast() :: Releasing multicast lock.");
|
2013-11-26 17:58:26 -04:30
|
|
|
if(multicastLock != null){
|
|
|
|
|
multicastLock.release();
|
|
|
|
|
multicastLock = null;
|
|
|
|
|
}
|
2013-11-21 08:10:12 -04:30
|
|
|
}
|
2014-01-24 12:24:51 -04:30
|
|
|
|
2014-04-10 17:55:31 -04:30
|
|
|
////////////////////////////////////
|
|
|
|
|
// CVProcessor interface methods. //
|
|
|
|
|
////////////////////////////////////
|
2014-01-24 12:24:51 -04:30
|
|
|
@Override
|
2014-04-30 13:05:36 -04:30
|
|
|
public CVMarkerData findMarkersInFrame(byte[] frame){
|
2014-03-11 18:31:51 -04:30
|
|
|
if(ocvOn){
|
|
|
|
|
int codes[] = new int[15];
|
2014-03-12 16:34:52 -04:30
|
|
|
Bitmap tFrame, mFrame;
|
|
|
|
|
|
|
|
|
|
tFrame = BitmapFactory.decodeByteArray(frame, 0, frame.length);
|
2014-01-24 12:24:51 -04:30
|
|
|
|
2014-03-12 16:34:52 -04:30
|
|
|
Mat inImg = new Mat();
|
|
|
|
|
Mat outImg = new Mat();
|
|
|
|
|
Utils.bitmapToMat(tFrame, inImg);
|
2014-01-24 12:24:51 -04:30
|
|
|
|
2014-04-10 17:55:31 -04:30
|
|
|
getMarkerCodesAndLocations(inImg.getNativeObjAddr(), outImg.getNativeObjAddr(), codes);
|
2014-03-11 18:31:51 -04:30
|
|
|
|
2014-04-30 13:05:36 -04:30
|
|
|
//Mat temp = new Mat();
|
|
|
|
|
//Imgproc.cvtColor(outImg, temp, Imgproc.COLOR_BGR2RGB);
|
2014-03-18 18:04:38 -04:30
|
|
|
|
2014-04-30 13:05:36 -04:30
|
|
|
mFrame = Bitmap.createBitmap(outImg.cols(), outImg.rows(), Bitmap.Config.RGB_565);
|
|
|
|
|
Utils.matToBitmap(outImg, mFrame);
|
2014-03-11 18:31:51 -04:30
|
|
|
mFrame.compress(CompressFormat.JPEG, 100, outputStream);
|
|
|
|
|
|
2014-04-30 13:05:36 -04:30
|
|
|
CVMarkerData data = new CVMarkerData();
|
2014-03-11 18:31:51 -04:30
|
|
|
data.outFrame = outputStream.toByteArray();
|
|
|
|
|
data.markerCodes = codes;
|
|
|
|
|
|
|
|
|
|
tFrame.recycle();
|
|
|
|
|
mFrame.recycle();
|
|
|
|
|
outputStream.reset();
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}else{
|
2014-04-30 13:05:36 -04:30
|
|
|
Gdx.app.debug(TAG, CLASS_NAME + ".findMarkersInFrame(): OpenCV is not ready or failed to load.");
|
2014-03-11 18:31:51 -04:30
|
|
|
return null;
|
|
|
|
|
}
|
2014-01-24 12:24:51 -04:30
|
|
|
}
|
2014-04-28 10:42:38 -04:30
|
|
|
|
|
|
|
|
@Override
|
2014-04-30 13:05:36 -04:30
|
|
|
public CVCalibrationData findCalibrationPattern(byte[] frame){
|
|
|
|
|
if(ocvOn){
|
|
|
|
|
boolean found;
|
|
|
|
|
float points[] = new float[ProjectConstants.CALIBRATION_PATTERN_POINTS * 2];
|
|
|
|
|
Bitmap tFrame, mFrame;
|
|
|
|
|
Mat inImg = new Mat(), outImg = new Mat();
|
|
|
|
|
CVCalibrationData data = new CVCalibrationData();
|
|
|
|
|
|
|
|
|
|
// Decode the input frame and convert it to an OpenCV Matrix.
|
|
|
|
|
tFrame = BitmapFactory.decodeByteArray(frame, 0, frame.length);
|
|
|
|
|
Utils.bitmapToMat(tFrame, inImg);
|
|
|
|
|
|
|
|
|
|
// Attempt to find the calibration pattern in the input frame.
|
|
|
|
|
found = findCalibrationPattern(inImg.getNativeObjAddr(), outImg.getNativeObjAddr(), points);
|
|
|
|
|
|
|
|
|
|
// Encode the output image as a JPEG image.
|
|
|
|
|
mFrame = Bitmap.createBitmap(outImg.cols(), outImg.rows(), Bitmap.Config.RGB_565);
|
|
|
|
|
Utils.matToBitmap(outImg, mFrame);
|
|
|
|
|
mFrame.compress(CompressFormat.JPEG, 100, outputStream);
|
|
|
|
|
|
|
|
|
|
// Prepare the output data structure.
|
|
|
|
|
data.outFrame = outputStream.toByteArray();
|
|
|
|
|
data.calibrationPoints = found ? points : null;
|
|
|
|
|
|
|
|
|
|
// Clean up memory.
|
|
|
|
|
tFrame.recycle();
|
|
|
|
|
mFrame.recycle();
|
|
|
|
|
outputStream.reset();
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}else{
|
|
|
|
|
Gdx.app.debug(TAG, CLASS_NAME + ".findCalibrationPattern(): OpenCV is not ready or failed to load.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public byte[] undistortFrame(byte[] frame){
|
|
|
|
|
if(ocvOn){
|
|
|
|
|
byte undistortedFrame[];
|
|
|
|
|
Bitmap tFrame, mFrame;
|
|
|
|
|
Mat inImg = new Mat(), outImg = new Mat();
|
|
|
|
|
|
|
|
|
|
// Decode the input frame and convert it to an OpenCV Matrix.
|
|
|
|
|
tFrame = BitmapFactory.decodeByteArray(frame, 0, frame.length);
|
|
|
|
|
Utils.bitmapToMat(tFrame, inImg);
|
|
|
|
|
|
|
|
|
|
// Apply the undistort correction to the input frame.
|
|
|
|
|
Imgproc.undistort(inImg, outImg, cameraMatrix, distortionCoeffs);
|
|
|
|
|
|
|
|
|
|
// Encode the output image as a JPEG image.
|
|
|
|
|
mFrame = Bitmap.createBitmap(outImg.cols(), outImg.rows(), Bitmap.Config.RGB_565);
|
|
|
|
|
Utils.matToBitmap(outImg, mFrame);
|
|
|
|
|
mFrame.compress(CompressFormat.JPEG, 100, outputStream);
|
|
|
|
|
|
|
|
|
|
// Prepare the return frame.
|
|
|
|
|
undistortedFrame = outputStream.toByteArray();
|
|
|
|
|
|
|
|
|
|
// Clean up memory.
|
|
|
|
|
tFrame.recycle();
|
|
|
|
|
mFrame.recycle();
|
|
|
|
|
outputStream.reset();
|
|
|
|
|
|
|
|
|
|
return undistortedFrame;
|
|
|
|
|
}else{
|
|
|
|
|
Gdx.app.debug(TAG, CLASS_NAME + ".undistortFrame(): OpenCV is not ready or failed to load.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-04-28 10:42:38 -04:30
|
|
|
}
|
2014-04-30 13:05:36 -04:30
|
|
|
}
|