Tutorial Cara Membuat Sensor Kamera Di Android Dengan OpenCV >> Kali ini saya akan membagikan tutorial membuat Sensor Kamera Di Android dengan menggunakan library OpenCV. Tahukah kalian apa itu OpenCV?? Open CV adalah library yang digunakan untuk menangkap,memproses, dan menampilkan gambar, baik 2D maupun 3D,kegunaan
utama library ini untuk pengenalan wajah,pengenalan gesture,deteksi gerakan,dan lain-lain yang berkaitan dengan computer vision.
utama library ini untuk pengenalan wajah,pengenalan gesture,deteksi gerakan,dan lain-lain yang berkaitan dengan computer vision.
Untuk menggunakan library OpenCV kalian bisa mengikuti langkah-langkah seperti berikut :
1. Download terlebih dahulu library openCV di http://opencv.org/downloads.html dan kalian bisa memilih platform Android
lalu ekstrak.
1. Download terlebih dahulu library openCV di http://opencv.org/downloads.html dan kalian bisa memilih platform Android
lalu ekstrak.
2. Saya asumsikan kalian sudah bisa mengimport library yaitu hanya dengan cara mengcopy library ke folder lib di android studio.
3. kemudian masuk ke project structure, klik kanan, pilih "Open Module Setting". Klik tab "dependency".
Dan pilih tanda hijau diatas kanan --> pilih module dependency dan Pilih appOpenCV.
Dan pilih tanda hijau diatas kanan --> pilih module dependency dan Pilih appOpenCV.
4. Biasanya lib ini berfungsi di android 5 ke atas, jadi kalian dapat menyeting targetSdkVersion 19.
5. Sekarang buat layout di layout activity_main.xml dengan mengisi seperti dibawah ini :
<FrameLayout xmlns:androoid="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.opencv.android.JavaCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androiid:visibility="gone"
android:id="@+id/tutorial1_activity_java_surface_view"
opencv:show_fps="true"
opencv:camera_id="any" />
<org.opencv.android.NativeCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androiid:visibility="gone"
android:id="@+id/tutorial1_activity_native_surface_view"
opencv:show_fps="true"
opencv:camera_id="any" />
</FrameLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.opencv.android.JavaCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androiid:visibility="gone"
android:id="@+id/tutorial1_activity_java_surface_view"
opencv:show_fps="true"
opencv:camera_id="any" />
<org.opencv.android.NativeCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
androiid:visibility="gone"
android:id="@+id/tutorial1_activity_native_surface_view"
opencv:show_fps="true"
opencv:camera_id="any" />
</FrameLayout>
Tambahkan permission camera di AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<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-feature android:name="android.hardware.camera" android:required="false"/>
<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"/>
package com.viavilab.alphabet;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.Toast;
/**
* Created by erza on 11/17/2015.
*/
public class MainActivity extends ActionBarActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
private static final String TAG = "OCVSample::Activity";
private CameraBridgeViewBase mOpenCvCameraView;
private boolean mIsJavaCamera = true;
private MenuItem mItemSwitchCamera = null;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){
@Override
public void onManagerConnected(int status){
switch (status){
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded succesfully");
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public MainActivity(){
Log.i(TAG,"Instantiated new " + this.getClass());
}
@Override
public void onCreate(Bundle savedInstanceState){
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
if(mIsJavaCamera)
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
else
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
@Override
public void onPause()
{
super.onPause();
if(mOpenCvCameraView !=null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume()
{
super.onResume();
OpenCVLoader.iniAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}
public void onDestroy(){
super.onDestroy();
if(mOpenCvCameraView !=null)
mOpenCvCameraView.disableView();
}
@Override
public boolean nCreateOptionsMenu(Menu menu){
Log.i(TAG, "called onCreateOptionsMenu");
mItemSwitchCamera = menu.add("Toggle Native/Java camera");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
String toastMessage = new String();
Log.i(TAG, "called onOptionsItemSelected; selected item : " item);
if(item == mItemSwitchCamera){
mOpenCvCameraView.setVisibility(SurfaceView.GONE);
mIsJavaCamera = !mIsJavaCamera;
if (mIsJavaCamera){
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
toastMessage = "Java Camera";
} else {
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
}
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();
Toast toast = Toast.makeText(this, toastMessage, Toast.LENGTH_LONG);
toast.show();
}
return true;
}
@Override
public void onCameraViewStarted(int width, int height){
}
@Override
public void onCameraViewStopped(){
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewframe inputFrame){
return inputFrame.rgba();
}
}
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.Toast;
/**
* Created by erza on 11/17/2015.
*/
public class MainActivity extends ActionBarActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
private static final String TAG = "OCVSample::Activity";
private CameraBridgeViewBase mOpenCvCameraView;
private boolean mIsJavaCamera = true;
private MenuItem mItemSwitchCamera = null;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){
@Override
public void onManagerConnected(int status){
switch (status){
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded succesfully");
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public MainActivity(){
Log.i(TAG,"Instantiated new " + this.getClass());
}
@Override
public void onCreate(Bundle savedInstanceState){
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
if(mIsJavaCamera)
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
else
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
@Override
public void onPause()
{
super.onPause();
if(mOpenCvCameraView !=null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume()
{
super.onResume();
OpenCVLoader.iniAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}
public void onDestroy(){
super.onDestroy();
if(mOpenCvCameraView !=null)
mOpenCvCameraView.disableView();
}
@Override
public boolean nCreateOptionsMenu(Menu menu){
Log.i(TAG, "called onCreateOptionsMenu");
mItemSwitchCamera = menu.add("Toggle Native/Java camera");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
String toastMessage = new String();
Log.i(TAG, "called onOptionsItemSelected; selected item : " item);
if(item == mItemSwitchCamera){
mOpenCvCameraView.setVisibility(SurfaceView.GONE);
mIsJavaCamera = !mIsJavaCamera;
if (mIsJavaCamera){
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
toastMessage = "Java Camera";
} else {
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_native_surface_view);
}
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();
Toast toast = Toast.makeText(this, toastMessage, Toast.LENGTH_LONG);
toast.show();
}
return true;
}
@Override
public void onCameraViewStarted(int width, int height){
}
@Override
public void onCameraViewStopped(){
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewframe inputFrame){
return inputFrame.rgba();
}
}
Jalankan Program aplikasi diusahakan jangan menggunakan emulator harus menggunakan device langsung karena jika menggunakan emulator fiture camera gak akan berfungsi, jika sudah sesuai dengan tutorial Tutorial Cara Membuat Sensor Kamera Di Android Dengan OpenCV harusnya ketika muncul kamera akan ada otomatis sensor-sensor dari kamera.
jika kalian masih mengalami kesulitan, silahkan bertanya pada kotak komentar dibawah.
Semoga bermanfaat !!!
mas, ada tutorial face recognition menggunakan library apa aja boleh, untuk android
BalasHapusdan mau nanya lagi, apakah open cv gak berlajan di android dibawah 5.0 ?
permisi. kalau opencv dipake untuk pengenalan sidik jari apakah bisa?
BalasHapusKomentar ini telah dihapus oleh pengarang.
BalasHapusKomentar ini telah dihapus oleh pengarang.
BalasHapusassalamualaikum mas, saya sudah buat programnya. tidak ada error. tetapi force close. itu kenapa ya ?? terima kasih
BalasHapus