diff --git a/android/.idea/misc.xml b/android/.idea/misc.xml index 1dc7455..7677dd7 100644 --- a/android/.idea/misc.xml +++ b/android/.idea/misc.xml @@ -29,7 +29,7 @@ - + \ No newline at end of file diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 23ab039..3789132 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + + + + + + + \ No newline at end of file diff --git a/android/app/src/main/java/com/krokogator/photoeat/MainActivity.java b/android/app/src/main/java/com/krokogator/photoeat/MainActivity.java index 79926fc..d48f55e 100644 --- a/android/app/src/main/java/com/krokogator/photoeat/MainActivity.java +++ b/android/app/src/main/java/com/krokogator/photoeat/MainActivity.java @@ -1,13 +1,96 @@ package com.krokogator.photoeat; -import android.support.v7.app.AppCompatActivity; +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.Uri; import android.os.Bundle; +import android.os.Environment; +import android.provider.MediaStore; +import android.support.v4.content.FileProvider; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.ImageView; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; public class MainActivity extends AppCompatActivity { + static final int REQUEST_TAKE_PHOTO = 1; + + ImageView photo_preview; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + Button btnCamera = (Button) findViewById(R.id.button_take_photo); + photo_preview = (ImageView) findViewById(R.id.mainImgView); + + btnCamera.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + dispatchTakePictureIntent(); +// System.out.println(getPic().getHeight()); + + } + }); + } + + static final int REQUEST_IMAGE_CAPTURE = 1; + + String mCurrentPhotoPath; + + private File createImageFile() throws IOException { + // Create an image file name + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); + String imageFileName = "JPEG_" + timeStamp + "_"; + File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); + File image = File.createTempFile( + imageFileName, /* prefix */ + ".jpg", /* suffix */ + storageDir /* directory */ + ); + + // Save a file: path for use with ACTION_VIEW intents + mCurrentPhotoPath = image.getAbsolutePath(); + return image; + } + + private void dispatchTakePictureIntent() { + Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + // Ensure that there's a camera activity to handle the intent + if (takePictureIntent.resolveActivity(getPackageManager()) != null) { + // Create the File where the photo should go + File photoFile = null; + try { + photoFile = createImageFile(); + } catch (IOException ex) { + // Error occurred while creating the File + } + // Continue only if the File was successfully created + if (photoFile != null) { + Uri photoURI = FileProvider.getUriForFile(this, + "com.krokogator.photoeat.android.fileprovider", + photoFile); + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); + startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); + } + } + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { + Intent previewPhotoIntent = new Intent(this, PhotoPreviewActivity.class); + previewPhotoIntent.putExtra("imagePath", mCurrentPhotoPath); + startActivity(previewPhotoIntent); + } } } diff --git a/android/app/src/main/java/com/krokogator/photoeat/PhotoPreviewActivity.java b/android/app/src/main/java/com/krokogator/photoeat/PhotoPreviewActivity.java new file mode 100644 index 0000000..e9b6d74 --- /dev/null +++ b/android/app/src/main/java/com/krokogator/photoeat/PhotoPreviewActivity.java @@ -0,0 +1,91 @@ +package com.krokogator.photoeat; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.media.ExifInterface; +import android.media.Image; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.widget.ImageView; + +import java.util.logging.Logger; + +public class PhotoPreviewActivity extends AppCompatActivity { + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.photo_previev); + + String imagePath = (String) getIntent().getExtras().get("imagePath"); + ImageView preview = findViewById(R.id.photo_preview); + preview.setImageBitmap(getPic(imagePath)); + } + + private Bitmap getPic(String picPath) { + // Get the dimensions of the View + int targetW = 800; + int targetH = 1200; + + // Get the dimensions of the bitmap + BitmapFactory.Options bmOptions = new BitmapFactory.Options(); + bmOptions.inJustDecodeBounds = true; + BitmapFactory.decodeFile(picPath, bmOptions); + int photoW = bmOptions.outWidth; + int photoH = bmOptions.outHeight; + + // Determine how much to scale down the image + int scaleFactor = Math.min(photoW/targetW, photoH/targetH); + + // Decode the image file into a Bitmap sized to fill the View + bmOptions.inJustDecodeBounds = false; + bmOptions.inSampleSize = scaleFactor; + bmOptions.inPurgeable = true; + + Bitmap bitmap = BitmapFactory.decodeFile(picPath, bmOptions); + bitmap = getCorrectlyRotated(picPath, bitmap); + + return bitmap; + } + + private Bitmap getCorrectlyRotated(String imagePath, Bitmap bitmap){ + // Rotate Image if Needed + try + { + // Determine Orientation + ExifInterface exif = new ExifInterface(imagePath); + int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); + + // Determine Rotation + int rotation = 0; + if (orientation == 6) rotation = 90; + else if (orientation == 3) rotation = 180; + else if (orientation == 8) rotation = 270; + + // Rotate Image if Necessary + if (rotation != 0) + { + // Create Matrix + Matrix matrix = new Matrix(); + matrix.postRotate(rotation); + + // Rotate Bitmap + Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + + // Pretend none of this ever happened! + bitmap.recycle(); + bitmap = rotated; + rotated = null; + } + } + catch (Exception e) + { + // TODO: Log Error Messages Here + } + + return bitmap; + } +} diff --git a/android/app/src/main/res/layout/activity_main.xml b/android/app/src/main/res/layout/activity_main.xml index 9cef12f..a983007 100644 --- a/android/app/src/main/res/layout/activity_main.xml +++ b/android/app/src/main/res/layout/activity_main.xml @@ -6,8 +6,14 @@ android:layout_height="match_parent" tools:context="com.krokogator.photoeat.MainActivity"> + +