Skip to content

Commit

Permalink
Fixed some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauszus committed Apr 30, 2018
1 parent 8c35295 commit 920fda4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* frame to the screen.
* The clients shall implement CvCameraViewListener.
*/
@SuppressWarnings({"unused", "UnnecessaryInterfaceModifier"})
public abstract class CameraBridgeViewBase extends SurfaceView implements SurfaceHolder.Callback {

private static final String TAG = "CameraBridge";
Expand All @@ -48,15 +49,15 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
private Bitmap mCacheBitmap;
private CvCameraViewListener2 mListener;
private boolean mSurfaceExist;
private Object mSyncObject = new Object();
private final Object mSyncObject = new Object();

protected int mFrameWidth;
protected int mFrameHeight;
protected int mMaxHeight;
protected int mMaxWidth;
protected float mScale = 0;
protected int mPreviewFormat = RGBA;
protected int mCameraIndex = CAMERA_ID_ANY;
protected int mCameraIndex;
protected boolean mEnabled;
protected FpsMeter mFpsMeter = null;

Expand All @@ -66,7 +67,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
public static final int RGBA = 1;
public static final int GRAY = 2;

public WindowManager mWindowManager;
private WindowManager mWindowManager;

public CameraBridgeViewBase(Context context, int cameraId) {
super(context);
Expand All @@ -80,13 +81,13 @@ public CameraBridgeViewBase(Context context, AttributeSet attrs) {
super(context, attrs);

int count = attrs.getAttributeCount();
Log.d(TAG, "Attr count: " + Integer.valueOf(count));
Log.d(TAG, "Attr count: " + count);

TypedArray styledAttrs = getContext().obtainStyledAttributes(attrs, R.styleable.CameraBridgeViewBase);
if (styledAttrs.getBoolean(R.styleable.CameraBridgeViewBase_show_fps, false))
enableFpsMeter();

mCameraIndex = styledAttrs.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1);
mCameraIndex = styledAttrs.getInt(R.styleable.CameraBridgeViewBase_camera_id, CAMERA_ID_ANY);

getHolder().addCallback(this);
mMaxWidth = MAX_UNSPECIFIED;
Expand Down Expand Up @@ -157,8 +158,9 @@ public interface CvCameraViewListener2 {
* TODO: pass the parameters specifying the format of the frame (BPP, YUV or RGB and etc)
*/
public Mat onCameraFrame(CvCameraViewFrame inputFrame);
};
}

@SuppressWarnings("WeakerAccess")
protected class CvCameraViewListenerAdapter implements CvCameraViewListener2 {
public CvCameraViewListenerAdapter(CvCameraViewListener oldStypeListener) {
mOldStyleListener = oldStypeListener;
Expand All @@ -183,7 +185,7 @@ public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
break;
default:
Log.e(TAG, "Invalid frame format! Only RGBA and Gray Scale are supported!");
};
}

return result;
}
Expand All @@ -194,7 +196,7 @@ public void setFrameFormat(int format) {

private int mPreviewFormat = RGBA;
private CvCameraViewListener mOldStyleListener;
};
}

/**
* This class interface is abstract representation of single frame from camera for onCameraFrame callback
Expand All @@ -211,7 +213,7 @@ public interface CvCameraViewFrame {
* This method returns single channel gray scale Mat with frame
*/
public Mat gray();
};
}

public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
Log.d(TAG, "call surfaceChanged event");
Expand All @@ -220,7 +222,7 @@ public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
mSurfaceExist = true;
checkCurrentState();
} else {
/** Surface changed. We need to stop camera and restart with new parameters */
/* Surface changed. We need to stop camera and restart with new parameters */
/* Pretend that old surface has been destroyed */
mSurfaceExist = false;
checkCurrentState();
Expand Down Expand Up @@ -280,9 +282,8 @@ public void disableFpsMeter() {

/**
*
* @param listener
* @param listener - set the CvCameraViewListener2
*/

public void setCvCameraViewListener(CvCameraViewListener2 listener) {
mListener = listener;
}
Expand Down Expand Up @@ -352,7 +353,7 @@ private void processEnterState(int state) {
mListener.onCameraViewStopped();
}
break;
};
}
}

private void processExitState(int state) {
Expand All @@ -364,7 +365,7 @@ private void processExitState(int state) {
case STOPPED:
onExitStoppedState();
break;
};
}
}

private void onEnterStoppedState() {
Expand Down Expand Up @@ -687,15 +688,16 @@ protected void AllocateCache()
public interface ListItemAccessor {
public int getWidth(Object obj);
public int getHeight(Object obj);
};
}

/**
* This helper method can be called by subclasses to select camera preview size.
* It goes over the list of the supported preview sizes and selects the maximum one which
* fits both values set via setMaxFrameSize() and surface frame allocated for this view
* @param supportedSizes
* @param surfaceWidth
* @param surfaceHeight
* @param supportedSizes - list of android.hardware.Camera.Size
* @param accessor - accessor used to get the width and height
* @param surfaceWidth - width of the surface
* @param surfaceHeight - height of the surface
* @return optimal frame size
*/
protected Size calculateCameraFrameSize(List<?> supportedSizes, ListItemAccessor accessor, int surfaceWidth, int surfaceHeight) {
Expand All @@ -711,8 +713,8 @@ protected Size calculateCameraFrameSize(List<?> supportedSizes, ListItemAccessor

if (width <= maxAllowedWidth && height <= maxAllowedHeight) {
if (width >= calcWidth && height >= calcHeight) {
calcWidth = (int) width;
calcHeight = (int) height;
calcWidth = width;
calcHeight = height;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Source: https://github.com/opencv/opencv/blob/master/modules/java/generator/src/java/android+JavaCameraView.java
// Modified to used modified CameraBridgeViewBase
// Based on: https://github.com/opencv/opencv/blob/master/modules/java/generator/android/java/org/opencv/android/JavaCameraView.java

package com.lauszus.facerecognitionapp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
Expand Down Expand Up @@ -43,9 +43,10 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb

protected Camera mCamera;
protected JavaCameraFrame[] mCameraFrame;
private SurfaceTexture mSurfaceTexture;
private int mPreviewFormat = ImageFormat.NV21;

private int previewFormat = ImageFormat.NV21;
@SuppressWarnings("FieldCanBeLocal") // This slows down the frame rate significantly
private SurfaceTexture mSurfaceTexture;

public static class JavaCameraSizeAccessor implements ListItemAccessor {

Expand All @@ -62,6 +63,7 @@ public int getHeight(Object obj) {
}
}

@SuppressWarnings("unused")
public JavaCameraView(Context context, int cameraId) {
super(context, cameraId);
}
Expand All @@ -70,6 +72,7 @@ public JavaCameraView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@SuppressLint("ObsoleteSdkInt")
protected boolean initializeCamera(int width, int height) {
Log.d(TAG, "Initialize java camera");
boolean result = true;
Expand All @@ -88,7 +91,7 @@ protected boolean initializeCamera(int width, int height) {
if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
boolean connected = false;
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(camIdx) + ")");
Log.d(TAG, "Trying to open camera with new open(" + camIdx + ")");
try {
mCamera = Camera.open(camIdx);
connected = true;
Expand Down Expand Up @@ -127,7 +130,7 @@ protected boolean initializeCamera(int width, int height) {
} else if (localCameraIndex == CAMERA_ID_FRONT) {
Log.e(TAG, "Front camera not found!");
} else {
Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(localCameraIndex) + ")");
Log.d(TAG, "Trying to open camera with new open(" + localCameraIndex + ")");
try {
mCamera = Camera.open(localCameraIndex);
} catch (RuntimeException e) {
Expand All @@ -150,13 +153,13 @@ protected boolean initializeCamera(int width, int height) {
/* Select the size that fits surface considering maximum size allowed */
Size frameSize = calculateCameraFrameSize(sizes, new JavaCameraSizeAccessor(), width, height);

params.setPreviewFormat(ImageFormat.NV21);

// See: http://stackoverflow.com/a/27233595/2175837
if (isEmulator()) // Check if we are using the Android emulator
/* Image format NV21 causes issues in the Android emulators */
if (isEmulator())
params.setPreviewFormat(ImageFormat.YV12);
else
params.setPreviewFormat(ImageFormat.NV21);

Log.d(TAG, "Set preview size to " + Integer.valueOf((int)frameSize.width) + "x" + Integer.valueOf((int)frameSize.height));
Log.d(TAG, "Set preview size to " + frameSize.width + "x" + frameSize.height);
params.setPreviewSize((int)frameSize.width, (int)frameSize.height);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && !android.os.Build.MODEL.equals("GT-I9100"))
Expand All @@ -170,7 +173,7 @@ protected boolean initializeCamera(int width, int height) {

mCamera.setParameters(params);
params = mCamera.getParameters();
previewFormat = params.getPreviewFormat();
mPreviewFormat = params.getPreviewFormat();

mFrameWidth = params.getPreviewSize().width;
mFrameHeight = params.getPreviewSize().height;
Expand Down Expand Up @@ -278,7 +281,7 @@ protected void disconnectCamera() {
synchronized (this) {
this.notify();
}
Log.d(TAG, "Wating for thread");
Log.d(TAG, "Waiting for thread");
if (mThread != null)
mThread.join();
} catch (InterruptedException e) {
Expand Down Expand Up @@ -314,13 +317,17 @@ public Mat gray() {

@Override
public Mat rgba() {
if (previewFormat == ImageFormat.NV21)
if (mPreviewFormat == ImageFormat.NV21)
Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2RGBA_NV21, 4);
else if (previewFormat == ImageFormat.YV12)
else if (mPreviewFormat == ImageFormat.YV12)
Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2RGB_I420, 4); // COLOR_YUV2RGBA_YV12 produces inverted colors
else
throw new IllegalArgumentException("Preview Format can be NV21 or YV12");

return mRgba;
}

@SuppressWarnings("WeakerAccess")
public JavaCameraFrame(Mat Yuv420sp, int width, int height) {
super();
mWidth = width;
Expand Down

0 comments on commit 920fda4

Please sign in to comment.