<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:screenOrientation="landscape">
<org.opencv.android.JavaCameraView
android:id="@+id/deteksi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
opencv:camera_id="any"
opencv:show_fps="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_margin="16dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginTop="260dp">
<Button
android:id="@+id/btnCek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COMPARE"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/hasil" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" >
<!-- View Title Label -->
<ImageView
android:id="@+id/Hasil_gambar"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dip"
android:text="Nama"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/judul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:text="Nama"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dip"
android:layout_height="wrap_content"
android:text="Kingdom"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/kingdom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dip"
android:layout_height="wrap_content"
android:text="Divisi"
android:layout_gravity="center_horizontal"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/divisi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dip"
android:layout_height="wrap_content"
android:text="Subdivisi"
android:layout_gravity="center_horizontal"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/subdivisi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dip"
android:layout_height="wrap_content"
android:text="Kelas"
android:layout_gravity="center_horizontal"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/kelas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dip"
android:layout_height="wrap_content"
android:text="Ordo"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/ordo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dip"
android:layout_height="wrap_content"
android:text="Famili"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/famili"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dip"
android:layout_height="wrap_content"
android:text="Genus"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/genus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_marginTop="15dip"
android:layout_height="wrap_content"
android:text="Spesies"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/spesies"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
</ScrollView>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fau.deteksidaun">
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/daun"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".HalamanUtama">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CaraPenggunaan" />
<activity android:name=".Tentang" />
<activity android:name=".Deteksi" />
<activity android:name=".HasilKlasifikasi"></activity>
</application>
</manifest>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.fau.deteksidaun;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.DMatch;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Scalar;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.imgproc.Imgproc;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
public class Deteksi extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{
String gab="";
int jd=10;
Button btnCek;
String[]arObject;
String[]arNama;
String hasilObject="";
private static final String TAG = "OCVSample::Activity";
private static final int REQUEST_PERMISSION = 100;
private int w, h;
private CameraBridgeViewBase mOpenCvCameraView;
TextView tvName;
Scalar RED = new Scalar(255, 0, 0);
Scalar GREEN = new Scalar(0, 255, 0);
FeatureDetector detector;
DescriptorExtractor descriptor;
DescriptorMatcher matcher;
Mat descriptors2;
MatOfKeyPoint keypoints2;
MatOfKeyPoint [] key1;
Mat desc1[],citra1[];
static {
if (!OpenCVLoader.initDebug())
Log.d("ERROR", "Unable to load OpenCV");
else
Log.d("SUCCESS", "OpenCV loaded");
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
Log.i(TAG, "OpenCV loaded successfully");
// mOpenCvCameraView.enableView();
try {
initializeOpenCVDependencies();
} catch (IOException e) {
e.printStackTrace();
}
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
private void initializeOpenCVDependencies() throws IOException {
mOpenCvCameraView.enableView();
detector = FeatureDetector.create(FeatureDetector.ORB);
descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
Button btnCek=(Button)findViewById(R.id.btnCek);
btnCek.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Deteksi.this, HasilKlasifikasi.class);
i.putExtra("hasil ", hasilObject);
startActivity(i);
}});
arObject=new String[jd];
arNama=new String[jd];
desc1=new Mat[jd];
citra1=new Mat[jd];
key1=new MatOfKeyPoint[jd];
arObject[0]="obj1.jpg";
arObject[1]="obj2.jpg";
arObject[2]="obj3.jpg";
arNama[0]="Object 1";
arNama[1]="Object 2";
arNama[2]="Object 3";
AssetManager assetManager = getAssets();
for(int k=0;k<jd;k++) {
citra1[k] = new Mat();
InputStream istr1 = assetManager.open(arObject[k]);//a.jpeg
Bitmap bitmap1 = BitmapFactory.decodeStream(istr1);
Utils.bitmapToMat(bitmap1, citra1[k] );
Imgproc.cvtColor(citra1[k] , citra1[k] , Imgproc.COLOR_RGB2GRAY);
citra1[k] .convertTo(citra1[k] , 0);
desc1[k] = new Mat();
key1[k] = new MatOfKeyPoint();
detector.detect(citra1[k] , key1[k]);
descriptor.compute(citra1[k] , key1[k], desc1[k]);
}
//============================================1
}
public Deteksi() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_deteksi);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION);
}
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.deteksi);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
@Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, Deteksi.this, mLoaderCallback);
} else {
Log.d(TAG, "OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onCameraViewStarted(int width, int height) {
w = width;
h = height;
}
@Override
public void onCameraViewStopped() {
}
public Mat recognize(Mat aInputFrame) {//aInputFrame=objek dari Kamera
double nmin = 10000000;
int indexKe=0;
Imgproc.cvtColor(aInputFrame, aInputFrame, Imgproc.COLOR_RGB2GRAY);
descriptors2 = new Mat();
keypoints2 = new MatOfKeyPoint();
detector.detect(aInputFrame, keypoints2);
descriptor.compute(aInputFrame, keypoints2, descriptors2);
MatOfDMatch matches = null;
Double max_dist = 0.0;
Double min_dist = 100.0;
int JM = 0;
matches = new MatOfDMatch();
for(int k=0;k<jd;k++){
if (citra1[k].type() == aInputFrame.type()) {
try {
matcher.match(desc1[k], descriptors2, matches);
List<DMatch> matchesList1 = matches.toList();
max_dist = 0.0;
min_dist = 100.0;
JM = matchesList1.size();
for (int i = 0; i < JM; i++) {
Double dist = (double) matchesList1.get(i).distance;
if (dist < min_dist)
min_dist = dist;
if (dist > max_dist)
max_dist = dist;
}
Log.d("MAXMIN15", "@Object1=" + max_dist + "@" + min_dist + "@" + (1.2 * min_dist));
if (min_dist < nmin) {
indexKe = k;
nmin = min_dist;
}
} catch (Exception ee) {
}
} else {
return aInputFrame;
}
}//loop
//=================================================================================3
matches = new MatOfDMatch();
if (citra1[indexKe].type() == aInputFrame.type()) {
try {
matcher.match(desc1[indexKe], descriptors2, matches);
} catch (Exception ee) {
}
} else {
return aInputFrame;
}
List<DMatch> matchesListOut = matches.toList();
JM = matchesListOut.size();
List<DMatch> listBest=null;
Mat imgBest=new Mat();
MatOfKeyPoint keyBest=null;
listBest=matchesListOut;
imgBest=citra1[indexKe];
keyBest=key1[indexKe];
hasilObject=arNama[indexKe];
//=================================================================================================LAST
int mm=0;
LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
for (int i = 0; i < JM; i++) {
if (listBest.get(i).distance <= (1.2 * min_dist)) {//1.5 * min_dist
good_matches.addLast(listBest.get(i));
mm++;
}
}
gab=gab+String.valueOf(mm)+"#";
Log.d("GAB","#"+gab+"+++++++++++++++++++++++++++++++++++++++++++++");
MatOfDMatch goodMatches = new MatOfDMatch();
goodMatches.fromList(good_matches);
Mat outputImg = new Mat();
MatOfByte drawnMatches = new MatOfByte();
if (aInputFrame.empty() || aInputFrame.cols() < 1 || aInputFrame.rows() < 1) {
return aInputFrame;
}
Features2d.drawMatches(imgBest, keyBest, aInputFrame, keypoints2, goodMatches, outputImg, GREEN, RED, drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);
Imgproc.resize(outputImg, outputImg, aInputFrame.size());
return outputImg;
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
return recognize(inputFrame.rgba());
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.fau.deteksidaun;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
public class HasilKlasifikasi extends AppCompatActivity {
String namafile="tes.jpg";
private AssetManager assetManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hasil_klasifikasi);
assetManager = getAssets();
Intent i = this.getIntent();
String daun=i.getStringExtra("hasil");
ImageView Hasil_gambar= (ImageView) findViewById(R.id.Hasil_gambar);
TextView txtJudul= (TextView) findViewById(R.id.judul);txtJudul.setText(nama);
TextView txtKingdom = (TextView) findViewById(R.id.kingdom);txtKingdom.setText(kingdom);
TextView txtdivisi= (TextView) findViewById(R.id.divisi);txtdivisi.setText(divisi);
TextView txtsubdivisi= (TextView) findViewById(R.id.subdivisi);;txtsubdivisi.setText(subdivisi);
TextView txtkelas= (TextView) findViewById(R.id.kelas);txtkelas.setText(kelas);
TextView txtordo= (TextView) findViewById(R.id.ordo);txtordo.setText(ordo);
TextView txtfamili= (TextView) findViewById(R.id.famili);txtfamili.setText(famili);
TextView txtgenus= (TextView) findViewById(R.id.genus);txtgenus.setText(genus);
TextView txtspesies= (TextView) findViewById(R.id.spesies);txtspesies.setText(spesies);
try {
InputStream is = assetManager.open(namafile);
Bitmap bitmap = BitmapFactory.decodeStream(is);
Hasil_gambar.setImageBitmap(bitmap);
} catch (IOException e) {
Log.e("Hasil_gambar", e.getMessage());
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Tidak ada komentar:
Posting Komentar