Attribure loading from layout improved.

OpenCV namespace added;
Default values for camera_id added;
Aditional constructor with cameraId added.
Resolution added to FPS message.
This commit is contained in:
Alexander Smorkalov 2012-11-27 10:31:19 +04:00
parent 8266eab8b4
commit 0efc32fc21
5 changed files with 51 additions and 6 deletions

View File

@ -1,5 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="show_fps" format="boolean"/>
<attr name="camera_index" format="integer"/>
</resources>
<declare-styleable name = "CameraBridgeViewBase" >
<attr name="show_fps" format="boolean"/>
<attr name="camera_id" format="integer" >
<enum name="any" value="-1" />
<enum name="back" value="0" />
<enum name="front" value="1" />
</attr>
</declare-styleable>
</resources>

View File

@ -2,6 +2,7 @@ package org.opencv.android;
import java.util.List;
import org.opencv.R;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.core.Size;
@ -11,6 +12,7 @@ import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
@ -55,12 +57,22 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
private Object mSyncObject = new Object();
public CameraBridgeViewBase(Context context, int cameraId) {
super(context);
mCameraIndex = cameraId;
}
public CameraBridgeViewBase(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs.getAttributeBooleanValue(null, "show_fps", false))
int count = attrs.getAttributeCount();
Log.d(TAG, "Attr count: " + Integer.valueOf(count));
TypedArray tmp = getContext().obtainStyledAttributes(attrs, R.styleable.CameraBridgeViewBase);
if (tmp.getBoolean(R.styleable.CameraBridgeViewBase_show_fps, false))
enableFpsMeter();
mCameraIndex = attrs.getAttributeIntValue(null,"camera_index", -1);
mCameraIndex = tmp.getInt(R.styleable.CameraBridgeViewBase_camera_id, -1);
getHolder().addCallback(this);
mMaxWidth = MAX_UNSPECIFIED;
@ -148,6 +160,7 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac
public void enableFpsMeter() {
if (mFpsMeter == null) {
mFpsMeter = new FpsMeter();
mFpsMeter.setResolution(mFrameWidth, mFrameHeight);
}
}

View File

@ -19,6 +19,8 @@ public class FpsMeter {
DecimalFormat twoPlaces = new DecimalFormat("0.00");
Paint paint;
boolean isInitialized = false;
int mWidth = 0;
int mHeight = 0;
public void init() {
step = 20;
@ -43,12 +45,20 @@ public class FpsMeter {
double fps = step * freq / (time - prevFrameTime);
prevFrameTime = time;
DecimalFormat twoPlaces = new DecimalFormat("0.00");
strfps = twoPlaces.format(fps) + " FPS";
if (mWidth != 0 && mHeight != 0)
strfps = twoPlaces.format(fps) + " FPS@" + Integer.valueOf(mWidth) + "x" + Integer.valueOf(mHeight);
else
strfps = twoPlaces.format(fps) + " FPS";
Log.i(TAG, strfps);
}
}
}
public void setResolution(int width, int height) {
mWidth = width;
mHeight = height;
}
public void draw(Canvas canvas, float offsetx, float offsety) {
Log.d(TAG, strfps);
canvas.drawText(strfps, 20 + offsetx, 10 + 50 + offsety, paint);

View File

@ -57,6 +57,10 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
}
}
public JavaCameraView(Context context, int cameraId) {
super(context, cameraId);
}
public JavaCameraView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.d(TAG, "Java camera view ctor");
@ -131,6 +135,10 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
mFrameWidth = params.getPreviewSize().width;
mFrameHeight = params.getPreviewSize().height;
if (mFpsMeter != null) {
mFpsMeter.setResolution(mFrameWidth, mFrameHeight);
}
int size = mFrameWidth * mFrameHeight;
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
mBuffer = new byte[size];

View File

@ -22,6 +22,10 @@ public class NativeCameraView extends CameraBridgeViewBase {
protected VideoCapture mCamera;
public NativeCameraView(Context context, int cameraId) {
super(context, cameraId);
}
public NativeCameraView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@ -98,6 +102,10 @@ public class NativeCameraView extends CameraBridgeViewBase {
mFrameWidth = (int)frameSize.width;
mFrameHeight = (int)frameSize.height;
if (mFpsMeter != null) {
mFpsMeter.setResolution(mFrameWidth, mFrameHeight);
}
AllocateCache();
mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, frameSize.width);