Code review comments applied

Sample renamed to CameraControl;
Picture taking added to show camera preview restart.
This commit is contained in:
Alexander Smorkalov 2012-11-29 12:51:49 +04:00
parent 86f7a357ae
commit d36f8b9eb3
12 changed files with 83 additions and 40 deletions

View File

@ -16,7 +16,7 @@ add_subdirectory(tutorial-1-addopencv)
add_subdirectory(tutorial-2-opencvcamera)
add_subdirectory(tutorial-3-native)
add_subdirectory(tutorial-4-mixed)
add_subdirectory(tutorial-5-customcamera)
add_subdirectory(tutorial-5-cameracontrol)
#hello-android sample
if(HAVE_opencv_highgui)

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>OpenCV Tutorial 5 - Custom camera</name>
<name>OpenCV Tutorial 5 - Camera Control</name>
<comment></comment>
<projects>
</projects>

View File

@ -9,7 +9,7 @@
android:icon="@drawable/icon"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<activity android:name="Sample5CustomCamera"
<activity android:name="Sample5CameraControl"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation">
@ -34,5 +34,6 @@
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

View File

@ -1,4 +1,4 @@
set(sample example-tutorial-5-customcamera)
set(sample example-tutorial-5-cameracontrol)
add_android_project(${sample} "${CMAKE_CURRENT_SOURCE_DIR}" LIBRARY_DEPS ${OpenCV_BINARY_DIR} SDK_TARGET 11 ${ANDROID_SDK_TARGET})
if(TARGET ${sample})

View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -3,7 +3,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent" >
<org.opencv.samples.tutorial5.CustomJavaCameraView
<org.opencv.samples.tutorial5.SampleJavaCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">OCV T5 Custom Camera</string>
<string name="app_name">OCV T5 Camera Control</string>
</resources>

View File

@ -11,16 +11,20 @@ import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
public class Sample5CustomCamera extends Activity implements CvCameraViewListener {
public class Sample5CameraControl extends Activity implements CvCameraViewListener, OnTouchListener {
private static final String TAG = "OCVSample::Activity";
private CustomJavaCameraView mOpenCvCameraView;
private SampleJavaCameraView mOpenCvCameraView;
private MenuItem[] mEffectMenuItems;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@ -31,6 +35,7 @@ public class Sample5CustomCamera extends Activity implements CvCameraViewListene
{
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
mOpenCvCameraView.setOnTouchListener(Sample5CameraControl.this);
} break;
default:
{
@ -40,7 +45,7 @@ public class Sample5CustomCamera extends Activity implements CvCameraViewListene
}
};
public Sample5CustomCamera() {
public Sample5CameraControl() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
@ -53,7 +58,7 @@ public class Sample5CustomCamera extends Activity implements CvCameraViewListene
setContentView(R.layout.tutorial5_surface_view);
mOpenCvCameraView = (CustomJavaCameraView) findViewById(R.id.tutorial5_activity_java_surface_view);
mOpenCvCameraView = (SampleJavaCameraView) findViewById(R.id.tutorial5_activity_java_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
@ -112,4 +117,11 @@ public class Sample5CustomCamera extends Activity implements CvCameraViewListene
mOpenCvCameraView.setEffect((String) item.getTitle());
return true;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG,"onTouch event");
mOpenCvCameraView.takePicture(Environment.getExternalStorageDirectory().getPath() + "/sample_picture.jpg");
return false;
}
}

View File

@ -0,0 +1,60 @@
package org.opencv.samples.tutorial5;
import java.io.FileOutputStream;
import java.util.List;
import org.opencv.android.JavaCameraView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.util.AttributeSet;
import android.util.Log;
public class SampleJavaCameraView extends JavaCameraView {
private static final String TAG = "Sample::SampleJavaCameraView";
public SampleJavaCameraView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public List<String> getEffectList() {
return mCamera.getParameters().getSupportedColorEffects();
}
public String getEffect() {
return mCamera.getParameters().getColorEffect();
}
public void setEffect(String effect) {
Camera.Parameters params = mCamera.getParameters();
params.setColorEffect(effect);
mCamera.setParameters(params);
}
public void takePicture(final String fileName) {
Log.i(TAG, "Tacking picture");
PictureCallback callback = new PictureCallback() {
private String mPictureFileName = fileName;
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.i(TAG, "Saving a bitmap to file");
Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
try {
FileOutputStream out = new FileOutputStream(mPictureFileName);
picture.compress(Bitmap.CompressFormat.JPEG, 90, out);
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
};
mCamera.takePicture(null, null, callback);
}
}

View File

@ -1,30 +0,0 @@
package org.opencv.samples.tutorial5;
import java.util.List;
import org.opencv.android.JavaCameraView;
import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
public class CustomJavaCameraView extends JavaCameraView {
public CustomJavaCameraView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public List<String> getEffectList() {
return mCamera.getParameters().getSupportedColorEffects();
}
public String getEffect() {
return mCamera.getParameters().getColorEffect();
}
public void setEffect(String effect) {
Camera.Parameters params = mCamera.getParameters();
params.setColorEffect(effect);
mCamera.setParameters(params);
}
}