Commit 280d8b02 authored by Pyone Thant Win's avatar Pyone Thant Win
Browse files

Density DB and minor updates

parent cccf806c
......@@ -29,4 +29,5 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.google.code.gson:gson:2.4'
implementation project(path: ':openCVLibrary3410')
}
......@@ -3,9 +3,12 @@
package="com.example.calorieestimator">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:name="android.hardware.camera2.full"
android:required="true" />
<application
......@@ -15,7 +18,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".foodInfo"></activity>
<activity android:name=".export_data"></activity>
<activity android:name=".foodInfo" />
<activity
android:name=".take_picture"
android:label="@string/title_activity_take_picture"
......
......@@ -56,7 +56,7 @@ public class MainActivity extends AppCompatActivity {
_btn_skip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, take_picture.class);
Intent intent = new Intent(MainActivity.this, export_data.class);
startActivity(intent);
}
});
......
package com.example.calorieestimator;
public class csvHelper {
private String fruit;
private float density;
public String getFruit(){
return fruit;
}
public void setFruit(String fruit){
this.fruit = fruit;
}
public float getDensity(){
return density;
}
public void setDensity(float density){
this.density = density;
}
}
package com.example.calorieestimator;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class export_data extends AppCompatActivity {
TextView value;
EditText _txt_fruitName;
Button _btn_next;
double fr_density;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_export_data);
_txt_fruitName = (EditText)findViewById(R.id.txt_fruitName);
_btn_next = (Button)findViewById(R.id.btn_next);
_btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fr_density = getCSVDATA(_txt_fruitName.getText().toString());
Intent intent = new Intent(export_data.this, foodInfo.class);
intent.putExtra("density", fr_density);
startActivity(intent);
}
});
}
private List<csvHelper> helperList = new ArrayList<>();
private double getCSVDATA(String input){
final InputStream inputStream = getResources().openRawResource(R.raw.density);
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, Charset.forName("UTF-8"))
);
String line = "";
double densityVal = 0;
try {
reader.readLine();
while ( (line = reader.readLine())!= null ){
String[] values = line.split(",");
String fruitName = values[0];
if (fruitName.compareToIgnoreCase(input) == 0){
densityVal = Double.parseDouble(values[1]);
}
}
} catch (IOException e){
Log.wtf("Activity", "Error: "+line, e);
e.printStackTrace();
}
return densityVal;
};
}
......@@ -24,7 +24,7 @@ import java.util.Arrays;
public class foodInfo extends AppCompatActivity {
TextView _api_result;
TextView _api_result, _textv2_density;
EditText _food_name;
Button _btn_calc;
......@@ -32,10 +32,12 @@ public class foodInfo extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_info);
Double density = getIntent().getExtras().getDouble("density");
_api_result = (TextView)findViewById(R.id.api_view);
_textv2_density = (TextView)findViewById(R.id.textView_density);
_food_name = (EditText)findViewById(R.id.food_name);
_btn_calc = (Button)findViewById(R.id.btn_calc);
_textv2_density.setText(density.toString());
_btn_calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
......
......@@ -15,7 +15,7 @@ import android.widget.Toast;
public class login extends AppCompatActivity {
SQLiteDatabase db;
SQLiteOpenHelper openHelper;
Button _btn_login;
Button _btn_login, _btn_skip;
EditText _user_email, _user_pswd;
Cursor cursor;
......@@ -27,6 +27,7 @@ public class login extends AppCompatActivity {
openHelper = new database_helper(this);
db = openHelper.getReadableDatabase();
_btn_login = (Button)findViewById(R.id.btn_login);
_btn_skip = (Button)findViewById(R.id.btn_skip);
_user_email = (EditText)findViewById(R.id.user_email);
_user_pswd = (EditText)findViewById(R.id.user_pswd);
_btn_login.setOnClickListener(new View.OnClickListener() {
......@@ -44,6 +45,13 @@ public class login extends AppCompatActivity {
}
}
});
_btn_skip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(login.this, export_data.class);
startActivity(intent);
}
});
}
/*private void validate(String username, String password){
if ((username == "Admin") && (password=="123abc")){
......
package com.example.calorieestimator;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Bundle;
import android.hardware.camera2.*;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.provider.MediaStore;
import android.util.Log;
import android.util.Size;
import android.util.SparseIntArray;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.security.Policy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class take_picture extends AppCompatActivity {
//Button _btn_cam;
private ImageView _imageView;
private static final int REQUEST_IMAGE_CAPTURE = 101;
Button _btn_cam;
private TextureView _imageTView;
Button _btn_next;
//Checking state orientation of output image
private static final SparseIntArray orientation = new SparseIntArray();
static {
orientation.append(Surface.ROTATION_0,90);
orientation.append(Surface.ROTATION_90,0);
orientation.append(Surface.ROTATION_180,270);
orientation.append(Surface.ROTATION_270,180);
}
private String cameraID;
private CameraDevice my_camera;
private CameraCaptureSession cameraCaptureSessions;
private CaptureRequest.Builder cameraRequestBuilder;
private Size imageDimensions;
private ImageReader imageReader;
//Save to FILE
private File file;
private static final int REQUEST_CAMERA_PERMISSION = 200;
private boolean mFlashSupported;
private Handler mBackgroundHandler;
private HandlerThread mBackgroundThread;
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
my_camera = cameraDevice;
createCameraPreview();
}
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
cameraDevice.close();
}
@Override
public void onError(@NonNull CameraDevice cameraDevice, int i) {
cameraDevice.close();
cameraDevice = null;
}
};
int focal_length;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_picture);
_imageView = findViewById(R.id.imageView);
_imageTView = findViewById(R.id.img_texture_view);
assert _imageTView != null;
_imageTView.setSurfaceTextureListener(textureListener);
_btn_cam = (Button)findViewById(R.id.btn_cam);
_btn_cam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
takePicture();
}
});
_btn_next = (Button)findViewById(R.id.btn_next);
_btn_next.setOnClickListener(new View.OnClickListener() {
@Override
......@@ -37,19 +119,223 @@ public class take_picture extends AppCompatActivity {
});
}
public void btn_cam(View view) {
Intent imageTakeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (imageTakeIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(imageTakeIntent, REQUEST_IMAGE_CAPTURE);
private void openCamera() {
CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
try{
cameraID = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraID);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimensions = map.getOutputSizes(SurfaceTexture.class)[0];
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},REQUEST_CAMERA_PERMISSION);
return;
}
manager.openCamera(cameraID, stateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
};
private void takePicture(){
if (my_camera == null)
return;
CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
/*try {
for (String camera_id : manager.getCameraIdList()) {
CameraCharacteristics chars = manager.getCameraCharacteristics(camera_id);
Size[] jpegTN = chars.get(CameraCharacteristics.JPEG_AVAILABLE_THUMBNAIL_SIZES);
for (int i = 0; i<jpegTN.length; i++) {
Log.d("CHARSSSSSSSSSS: ", String.valueOf(jpegTN[i]));
}
}
}catch (CameraAccessException e){
e.printStackTrace();
}*/
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(my_camera.getId());
Size[] jpegSizes = null;
if (characteristics != null){
jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
.getOutputSizes(ImageFormat.JPEG);
}
Log.e("Sizesssss: ",characteristics.toString());
int width = 320;
int height = 240;
if (jpegSizes != null && jpegSizes.length > 0){
width = jpegSizes[0].getWidth();
height= jpegSizes[0].getHeight();
}
final ImageReader reader = ImageReader.newInstance(width, height,ImageFormat.RAW_SENSOR, 1);
List<Surface> outputSurface = new ArrayList<>(2);
outputSurface.add(reader.getSurface());
outputSurface.add(new Surface(_imageTView.getSurfaceTexture()));
final CaptureRequest.Builder captureBuilder = my_camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(reader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, orientation.get(rotation));
file = new File(Environment.getExternalStorageDirectory()+"/"+ UUID.randomUUID().toString() +".jpg");
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
Image image = null;
try{
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally {
if (image != null)
image.close();
}
}
private void save(byte[] bytes) throws IOException {
OutputStream outputStream = null;
try{
outputStream = new FileOutputStream(file);
outputStream.write(bytes);
} finally {
if (outputStream != null)
outputStream.close();
}
}
};
reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Toast.makeText(take_picture.this, "Saved "+file, Toast.LENGTH_SHORT).show();
createCameraPreview();
}
};
my_camera.createCaptureSession(outputSurface, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
try {
cameraCaptureSession.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
}
}, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void createCameraPreview() {
try {
SurfaceTexture texture = _imageTView.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(imageDimensions.getWidth(), imageDimensions.getHeight());
Surface surface = new Surface(texture);
cameraRequestBuilder = my_camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
cameraRequestBuilder.addTarget(surface);
my_camera.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
if (my_camera == null)
return;
cameraCaptureSessions = cameraCaptureSession;
updatePreview();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
Toast.makeText(take_picture.this, "Changed", Toast.LENGTH_SHORT).show();
}
}, null);
} catch ( CameraAccessException e) {
e.printStackTrace();
}
}
private void updatePreview() {
if (my_camera == null)
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
cameraRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);
try {
cameraCaptureSessions.setRepeatingRequest(cameraRequestBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e){
e.printStackTrace();
}
}
TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
openCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
_imageView.setImageBitmap(imageBitmap);
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION){
if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "You cannot use camera without permission", Toast.LENGTH_SHORT).show();
finish();
}
}
}
@Override
protected void onResume() {
super.onResume();
startBackgroundThread();
if (_imageTView.isAvailable())
openCamera();
else
_imageTView.setSurfaceTextureListener(textureListener);
}
@Override
protected void onPause() {
mBackgroundThread.quitSafely();
try{
mBackgroundThread.join();
mBackgroundThread = null;
mBackgroundHandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
super.onPause();
}
private void startBackgroundThread() {
mBackgroundThread = new HandlerThread("Camera background: ");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".export_data">
<TextView
android:id="@+id/textView3"
android:layout_width="235dp"
android:layout_height="62dp"
android:layout_marginStart="88dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="88dp"
android:layout_marginBottom="10dp"
android:text="Please input the fruit name to get started: "
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/txt_fruitName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"