Attribure loading from layout improved.
authorAlexander Smorkalov <alexander.smorkalov@itseez.com>
Tue, 27 Nov 2012 06:31:19 +0000 (10:31 +0400)
committerAlexander Smorkalov <alexander.smorkalov@itseez.com>
Tue, 27 Nov 2012 08:47:41 +0000 (12:47 +0400)
OpenCV namespace added;
Default values for camera_id added;
Aditional constructor with cameraId added.
Resolution added to FPS message.

modules/java/android_lib/res/values/attrs.xml
modules/java/generator/src/java/android+CameraBridgeViewBase.java
modules/java/generator/src/java/android+FpsMeter.java
modules/java/generator/src/java/android+JavaCameraView.java
modules/java/generator/src/java/android+NativeCameraView.java

index 0294ad6..0cdf109 100644 (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>
\ No newline at end of file
+    <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>
index c267df9..c98574b 100644 (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);
         }
     }
 
index 858d95a..d15b87a 100644 (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);
index f41cc8e..ce50d66 100644 (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];
index 1a92d00..dad3cb9 100644 (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);