From 2eb4df3e637edfe5a8369583b79192602bfda151 Mon Sep 17 00:00:00 2001 From: zigimi Date: Sat, 19 Feb 2022 17:44:54 +0900 Subject: [PATCH 1/2] edited MakePopUp, edited TakeCard --- Card/app/src/main/AndroidManifest.xml | 7 + .../com/example/card/MakePopUpActivity.java | 86 +++++++++- .../com/example/card/TakeCardActivity.java | 147 +++++++++++++++++- Card/app/src/main/res/xml/file_paths.xml | 19 +++ 4 files changed, 254 insertions(+), 5 deletions(-) create mode 100644 Card/app/src/main/res/xml/file_paths.xml diff --git a/Card/app/src/main/AndroidManifest.xml b/Card/app/src/main/AndroidManifest.xml index f660e73..a5fac4c 100644 --- a/Card/app/src/main/AndroidManifest.xml +++ b/Card/app/src/main/AndroidManifest.xml @@ -2,6 +2,10 @@ + + + + diff --git a/Card/app/src/main/java/com/example/card/MakePopUpActivity.java b/Card/app/src/main/java/com/example/card/MakePopUpActivity.java index 5a8ed41..8bbff5b 100644 --- a/Card/app/src/main/java/com/example/card/MakePopUpActivity.java +++ b/Card/app/src/main/java/com/example/card/MakePopUpActivity.java @@ -1,14 +1,35 @@ package com.example.card; import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.Matrix; +import android.media.ExifInterface; +import android.net.Uri; import android.os.Bundle; +import android.os.Environment; +import android.provider.MediaStore; import android.view.View; import android.widget.Button; +import androidx.activity.result.ActivityResult; +import androidx.activity.result.ActivityResultCallback; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.FileProvider; + +import com.gun0912.tedpermission.PermissionListener; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; public class MakePopUpActivity extends AppCompatActivity { private Button btn_addInfo, btn_takeCard; + Uri providerURI; + String imageFilePath; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,13 +50,70 @@ public void onClick(View view) { btn_takeCard.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Intent intent = new Intent(MakePopUpActivity.this, TakeCardActivity.class); - startActivity(intent); + /*권한 설정 동의 안내 + TedPermission.with(getApplicationContext()) + .setPermissionListener(permissionListener) + .setRationaleMessage("카메라를 사용하기 위하여 접근 권한이 필요합니다.") + .setDeniedMessage("[설정] > [권한] 에서 권한을 허용할 수 있습니다.") + .setPermissions(Manifest.permission.CAMERA) + .check();*/ + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + if (intent.resolveActivity(getPackageManager()) != null) { + File photoFile = null; + try { + photoFile = createImageFile(); + } catch (IOException ex) { + + } + if (photoFile != null) { + providerURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName(), photoFile); + intent.putExtra(MediaStore.EXTRA_OUTPUT, providerURI); + launcher.launch(intent); + } + } } }); + } + PermissionListener permissionListener = new PermissionListener() { + @Override + public void onPermissionGranted() { + //Toast.makeText(getApplicationContext(), "권한이 허용됨", Toast.LENGTH_SHORT).show(); + } - } + @Override + public void onPermissionDenied(List deniedPermissions) { + //Toast.makeText(getApplicationContext(), "권한이 거부됨", Toast.LENGTH_SHORT).show(); + } + }; + + ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), + new ActivityResultCallback() + { + @Override + public void onActivityResult(ActivityResult result) + { + if (result.getResultCode() == RESULT_OK) + { + Intent intent = new Intent(MakePopUpActivity.this, TakeCardActivity.class); + intent.putExtra("str_uri", imageFilePath); + startActivity(intent); + } + } + }); + private File createImageFile() throws IOException { + 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 */ + ); + imageFilePath = image.getAbsolutePath(); + return image; + } -} +} \ No newline at end of file diff --git a/Card/app/src/main/java/com/example/card/TakeCardActivity.java b/Card/app/src/main/java/com/example/card/TakeCardActivity.java index ed7f5f8..b8c0ed6 100644 --- a/Card/app/src/main/java/com/example/card/TakeCardActivity.java +++ b/Card/app/src/main/java/com/example/card/TakeCardActivity.java @@ -1,6 +1,151 @@ package com.example.card; +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.media.ExifInterface; +import android.net.Uri; +import android.os.Bundle; +import android.os.Environment; +import android.provider.MediaStore; +import android.view.View; +import android.widget.Button; +import android.widget.ImageView; + +import androidx.activity.result.ActivityResult; +import androidx.activity.result.ActivityResultCallback; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.FileProvider; + +import com.gun0912.tedpermission.PermissionListener; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; public class TakeCardActivity extends AppCompatActivity { -} + ImageView imgView; + Button btn_cancel; + Uri providerURI; + String imageFilePath; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_takecard); + + imgView = findViewById(R.id.imgView); + btn_cancel = findViewById(R.id.btn_cancel); + + Intent intent = getIntent(); + imageFilePath = intent.getStringExtra("str_uri"); + + Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath); + ExifInterface exif = null; + + try { + exif = new ExifInterface(imageFilePath); + } catch (IOException e) { + e.printStackTrace(); + } + + int exifOrientation; + int exifDegree; + + if (exif != null) { + exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + exifDegree = exifOrientationToDegrees(exifOrientation); + } else { + exifDegree = 0; + } + imgView.setImageBitmap(rotate(bitmap, exifDegree)); + + btn_cancel.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + if (intent.resolveActivity(getPackageManager()) != null) { + File photoFile = null; + try { + photoFile = createImageFile(); + } catch (IOException ex) { + + } + if (photoFile != null) { + providerURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName(), photoFile); + intent.putExtra(MediaStore.EXTRA_OUTPUT, providerURI); + launcher.launch(intent); + } + } + } + }); + } + + ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), + new ActivityResultCallback() + { + @Override + public void onActivityResult(ActivityResult result) + { + if (result.getResultCode() == RESULT_OK) + { + Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath); + ExifInterface exif = null; + + try { + exif = new ExifInterface(imageFilePath); + } catch (IOException e) { + e.printStackTrace(); + } + + int exifOrientation; + int exifDegree; + + if (exif != null) { + exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); + exifDegree = exifOrientationToDegrees(exifOrientation); + } else { + exifDegree = 0; + } + imgView.setImageBitmap(rotate(bitmap, exifDegree)); + } + } + }); + + private File createImageFile() throws IOException { + 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 */ + ); + imageFilePath = image.getAbsolutePath(); + return image; + } + + private int exifOrientationToDegrees(int exifOrientation) { + if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { + return 90; + } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { + return 180; + } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { + return 270; + } + return 0; + } + + private Bitmap rotate(Bitmap bitmap, float degree) { + Matrix matrix = new Matrix(); + matrix.postRotate(degree); + return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + } + +} \ No newline at end of file diff --git a/Card/app/src/main/res/xml/file_paths.xml b/Card/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..4025771 --- /dev/null +++ b/Card/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,19 @@ + + + + + + + + + \ No newline at end of file From 4c3bd46aa3ab6c9f49647d1382f084d8cbcc477c Mon Sep 17 00:00:00 2001 From: zigimi Date: Sat, 19 Feb 2022 18:13:03 +0900 Subject: [PATCH 2/2] edited MakePopUp, edited TakeCard --- Card/.idea/misc.xml | 15 ++++---- Card/app/src/main/AndroidManifest.xml | 13 +++++++ .../java/com/example/card/MainActivity.java | 2 +- .../src/main/res/layout/activity_takecard.xml | 36 +++++++++++++++++++ 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 Card/app/src/main/res/layout/activity_takecard.xml diff --git a/Card/.idea/misc.xml b/Card/.idea/misc.xml index 9f39d58..f98d75b 100644 --- a/Card/.idea/misc.xml +++ b/Card/.idea/misc.xml @@ -3,12 +3,15 @@ + + + + + + + \ No newline at end of file diff --git a/Card/app/src/main/java/com/example/card/MainActivity.java b/Card/app/src/main/java/com/example/card/MainActivity.java index 000f78c..e286e36 100644 --- a/Card/app/src/main/java/com/example/card/MainActivity.java +++ b/Card/app/src/main/java/com/example/card/MainActivity.java @@ -28,7 +28,7 @@ protected void onCreate(Bundle savedInstanceState) { btn_make.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - Intent intent = new Intent(MainActivity.this, MyCardActivity.class); + Intent intent = new Intent(MainActivity.this, MakePopUpActivity.class); startActivity(intent); } }); diff --git a/Card/app/src/main/res/layout/activity_takecard.xml b/Card/app/src/main/res/layout/activity_takecard.xml new file mode 100644 index 0000000..11124c4 --- /dev/null +++ b/Card/app/src/main/res/layout/activity_takecard.xml @@ -0,0 +1,36 @@ + + + + + +