mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 06:03:15 +08:00
Added one more Android sample (using OpenCV from native)
This commit is contained in:
parent
724a3c1473
commit
4b61683071
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample 1 Java</name>
|
||||
<name>Sample 1 Java API</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
|
7
samples/android/2-native/.classpath
Normal file
7
samples/android/2-native/.classpath
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="gen"/>
|
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
33
samples/android/2-native/.project
Normal file
33
samples/android/2-native/.project
Normal file
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Sample 2 Native API</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -0,0 +1,5 @@
|
||||
#Wed Jun 29 04:36:40 MSD 2011
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
|
||||
org.eclipse.jdt.core.compiler.compliance=1.5
|
||||
org.eclipse.jdt.core.compiler.source=1.5
|
29
samples/android/2-native/AndroidManifest.xml
Normal file
29
samples/android/2-native/AndroidManifest.xml
Normal file
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.opencv.samples.s2"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
|
||||
<supports-screens android:resizeable="true"
|
||||
android:smallScreens="true"
|
||||
android:normalScreens="true"
|
||||
android:largeScreens="true"
|
||||
android:anyDensity="true" />
|
||||
|
||||
<application android:label="@string/app_name" android:icon="@drawable/icon">
|
||||
<activity android:name="Sample2Native"
|
||||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
<uses-sdk android:minSdkVersion="8" />
|
||||
|
||||
<uses-permission android:name="android.permission.CAMERA"/>
|
||||
<uses-feature android:name="android.hardware.camera" />
|
||||
<uses-feature android:name="android.hardware.camera.autofocus" />
|
||||
|
||||
</manifest>
|
11
samples/android/2-native/default.properties
Normal file
11
samples/android/2-native/default.properties
Normal file
@ -0,0 +1,11 @@
|
||||
# This file is automatically generated by Android Tools.
|
||||
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||
#
|
||||
# This file must be checked in Version Control Systems.
|
||||
#
|
||||
# To customize properties used by the Ant build system use,
|
||||
# "build.properties", and override values to adapt the script to your
|
||||
# project structure.
|
||||
|
||||
# Project target.
|
||||
target=android-8
|
16
samples/android/2-native/jni/Android.mk
Normal file
16
samples/android/2-native/jni/Android.mk
Normal file
@ -0,0 +1,16 @@
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
OPENCV_MK_BUILD_PATH:=../../../../android/build/OpenCV.mk
|
||||
ifeq ("$(wildcard $(OPENCV_MK_BUILD_PATH))","")
|
||||
include $(TOOLCHAIN_PREBUILT_ROOT)/user/share/OpenCV/OpenCV.mk
|
||||
else
|
||||
include $(OPENCV_MK_BUILD_PATH)
|
||||
endif
|
||||
|
||||
LOCAL_MODULE := native_sample
|
||||
LOCAL_SRC_FILES := jni_part.cpp
|
||||
LOCAL_LDLIBS += -llog -ldl
|
||||
|
||||
include $(BUILD_SHARED_LIBRARY)
|
3
samples/android/2-native/jni/Application.mk
Normal file
3
samples/android/2-native/jni/Application.mk
Normal file
@ -0,0 +1,3 @@
|
||||
APP_STL := gnustl_static
|
||||
APP_CPPFLAGS := -frtti -fexceptions
|
||||
APP_ABI := armeabi-v7a
|
33
samples/android/2-native/jni/jni_part.cpp
Normal file
33
samples/android/2-native/jni/jni_part.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <jni.h>
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/imgproc/imgproc.hpp>
|
||||
#include <opencv2/features2d/features2d.hpp>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
extern "C" {
|
||||
JNIEXPORT void JNICALL Java_org_opencv_samples_s2_Sample2View_FindFeatures(JNIEnv* env, jobject thiz, jint width, jint height, jbyteArray yuv, jintArray rgba)
|
||||
{
|
||||
jbyte* _yuv = env->GetByteArrayElements(yuv, 0);
|
||||
jint* _rgba = env->GetIntArrayElements(rgba, 0);
|
||||
|
||||
Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);
|
||||
Mat mrgba(height, width, CV_8UC4, (unsigned char *)_rgba);
|
||||
Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv);
|
||||
|
||||
cvtColor(myuv, mrgba, CV_YUV420i2BGR, 4);
|
||||
|
||||
vector<KeyPoint> v;
|
||||
|
||||
FastFeatureDetector detector(50);
|
||||
detector.detect(mgray, v);
|
||||
for( size_t i = 0; i < v.size(); i++ )
|
||||
circle(mrgba, Point(v[i].pt.x, v[i].pt.y), 10, Scalar(0,0,255,255));
|
||||
|
||||
env->ReleaseIntArrayElements(rgba, _rgba, 0);
|
||||
env->ReleaseByteArrayElements(yuv, _yuv, 0);
|
||||
}
|
||||
|
||||
}
|
BIN
samples/android/2-native/res/drawable/icon.png
Normal file
BIN
samples/android/2-native/res/drawable/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.6 KiB |
4
samples/android/2-native/res/values/strings.xml
Normal file
4
samples/android/2-native/res/values/strings.xml
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">Sample 2: Native API</string>
|
||||
</resources>
|
@ -0,0 +1,15 @@
|
||||
package org.opencv.samples.s2;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.Window;
|
||||
|
||||
public class Sample2Native extends Activity {
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
setContentView(new Sample2View(this));
|
||||
}
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
package org.opencv.samples.s2;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.hardware.Camera;
|
||||
import android.hardware.Camera.PreviewCallback;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class Sample2View extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||
private static final String TAG = "Sample2Native::View";
|
||||
|
||||
private Camera mCamera;
|
||||
private SurfaceHolder mHolder;
|
||||
private int mFrameWidth;
|
||||
private int mFrameHeight;
|
||||
private byte[] mFrame;
|
||||
private boolean mThreadRun;
|
||||
|
||||
public Sample2View(Context context) {
|
||||
super(context);
|
||||
mHolder = getHolder();
|
||||
mHolder.addCallback(this);
|
||||
}
|
||||
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
if ( mCamera != null) {
|
||||
Camera.Parameters params = mCamera.getParameters();
|
||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||
mFrameWidth = width;
|
||||
mFrameHeight = height;
|
||||
|
||||
//selecting optimal camera preview size
|
||||
{
|
||||
double minDiff = Double.MAX_VALUE;
|
||||
for (Camera.Size size : sizes) {
|
||||
if (Math.abs(size.height - height) < minDiff) {
|
||||
mFrameWidth = size.width;
|
||||
mFrameHeight = size.height;
|
||||
minDiff = Math.abs(size.height - height);
|
||||
}
|
||||
}
|
||||
}
|
||||
params.setPreviewSize(mFrameWidth, mFrameHeight);
|
||||
mCamera.setParameters(params);
|
||||
mCamera.startPreview();
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
mCamera = Camera.open();
|
||||
mCamera.setPreviewCallback(
|
||||
new PreviewCallback() {
|
||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||
synchronized(Sample2View.this) {
|
||||
mFrame = data;
|
||||
Sample2View.this.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
(new Thread(this)).start();
|
||||
}
|
||||
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
mThreadRun = false;
|
||||
if(mCamera != null) {
|
||||
synchronized(Sample2View.this) {
|
||||
mCamera.stopPreview();
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
mThreadRun = true;
|
||||
Log.i(TAG, "Starting thread");
|
||||
while(mThreadRun) {
|
||||
byte[] data = null;
|
||||
synchronized(this) {
|
||||
try {
|
||||
this.wait();
|
||||
data = mFrame;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
int frameSize = mFrameWidth*mFrameHeight;
|
||||
int[] rgba = new int[frameSize];
|
||||
|
||||
FindFeatures(mFrameWidth, mFrameHeight, data, rgba);
|
||||
|
||||
Bitmap bmp = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888);
|
||||
bmp.setPixels(rgba, 0/*offset*/, mFrameWidth /*stride*/, 0, 0, mFrameWidth, mFrameHeight);
|
||||
|
||||
Canvas canvas = mHolder.lockCanvas();
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth()-mFrameWidth)/2, (canvas.getHeight()-mFrameHeight)/2, null);
|
||||
mHolder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
public native void FindFeatures(int width, int height, byte yuv[], int[] rgba);
|
||||
|
||||
static {
|
||||
System.loadLibrary("native_sample");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user