Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 개발강의
- 안드로이드 서비스
- 홍드로이드 강의
- 안드로이드 네비게이션 메뉴
- 안드로이드 예제
- 앱 만들기
- 앱 만드는 법
- hongdroid
- 홍드로이드
- 플러터
- 개발자
- android tutorial
- 코딩
- Android Java
- 자바 튜토리얼
- IOS
- 안드로이드 앱 만들기
- android studio 앱 만드는 법
- 자바
- Android
- 안드로이드 스튜디오
- 안드로이드 코딩 기초
- java
- 안드로이드
- 안드로이드 튜토리얼
- android example
- 코틀린
- 안드로이드 기초
- Android Studio
- flutter
Archives
- Today
- Total
홍드로이드의 야매코딩
#11 안드로이드 스튜디오 카메라 (Camera) 예제 [ 홍드로이드 ] 본문
[build.gradle (Module: app)]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId "com.example.cameraexample" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'gun0912.ted:tedpermission:2.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' } | cs |
[AndroidManifest.xml]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.cameraexample"> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:authorities="com.example.cameraexample" android:name="androidx.core.content.FileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> </application> </manifest> | cs |
[activity_main.xml]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <ImageView android:id="@+id/iv_result" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/btn_capture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="촬영" /> </LinearLayout> </LinearLayout> | cs |
[file_paths.xml]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <cache-path name="cache" path="." /> <!--Context.getCacheDir() 내부 저장소--> <files-path name="files" path="." /> <!--Context.getFilesDir() 내부 저장소--> <external-path name="external" path="."/> <!-- Environment.getExternalStorageDirectory() 외부 저장소--> <external-cache-path name="external-cache" path="."/> <!-- Context.getExternalCacheDir() 외부 저장소--> <external-files-path name="external-files" path="."/> <!-- Context.getExternalFilesDir() 외부 저장소--> </paths> | cs |
[MediaScanner.java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | package com.example.cameraexample; import android.content.Context; import android.media.MediaScannerConnection; import android.net.Uri; import android.text.TextUtils; /** * 이미지 저장 후 미디어 스캐닝을 수행해줄 때 사용하는 유틸 클래스 */ public class MediaScanner { private Context mContext; private static volatile MediaScanner mMediaInstance = null; private MediaScannerConnection mMediaScanner; //private MediaScannerConnection.MediaScannerConnectionClient mMediaScannerClient; private String mFilePath; public static MediaScanner getInstance( Context context ) { if( null == context ) return null; if( null == mMediaInstance ) mMediaInstance = new MediaScanner( context ); return mMediaInstance; } public static void releaseInstance() { if ( null != mMediaInstance ) { mMediaInstance = null; } } private MediaScanner(Context context) { mContext = context; mFilePath = ""; MediaScannerConnection.MediaScannerConnectionClient mediaScanClient; mediaScanClient = new MediaScannerConnection.MediaScannerConnectionClient(){ @Override public void onMediaScannerConnected() { mMediaScanner.scanFile(mFilePath, null); // mFilePath = path; } @Override public void onScanCompleted(String path, Uri uri) { System.out.println("::::MediaScan Success::::"); mMediaScanner.disconnect(); } }; mMediaScanner = new MediaScannerConnection(mContext, mediaScanClient); } public void mediaScanning(final String path) { if( TextUtils.isEmpty(path) ) return; mFilePath = path; if( !mMediaScanner.isConnected() ) mMediaScanner.connect(); //mMediaScanner.scanFile( path,null ); } } | cs |
[MainActivity.java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | package com.example.cameraexample; import android.Manifest; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ExifInterface; import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import androidx.core.content.FileProvider; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import com.gun0912.tedpermission.PermissionListener; import com.gun0912.tedpermission.TedPermission; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Locale; import static android.os.Environment.DIRECTORY_PICTURES; public class MainActivity extends AppCompatActivity { private static final int REQUEST_IMAGE_CAPTURE = 672; private String imageFilePath; private Uri photoUri; private MediaScanner mMediaScanner; // 사진 저장 시 갤러리 폴더에 바로 반영사항을 업데이트 시켜주려면 이 것이 필요하다(미디어 스캐닝) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 사진 저장 후 미디어 스캐닝을 돌려줘야 갤러리에 반영됨. mMediaScanner = MediaScanner.getInstance(getApplicationContext()); // 권한 체크 TedPermission.with(getApplicationContext()) .setPermissionListener(permissionListener) .setRationaleMessage("카메라 권한이 필요합니다.") .setDeniedMessage("거부하셨습니다.") .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA) .check(); findViewById(R.id.btn_capture).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { File photoFile = null; try { photoFile = createImageFile(); } catch (IOException e) { } if (photoFile != null) { photoUri = FileProvider.getUriForFile(getApplicationContext(), getPackageName(), photoFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); } } } }); } private File createImageFile() throws IOException { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "TEST_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, ".jpg", storageDir ); imageFilePath = image.getAbsolutePath(); return image; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == 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 = exifOrientationToDegress(exifOrientation); } else { exifDegree = 0; } String result = ""; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HHmmss", Locale.getDefault() ); Date curDate = new Date(System.currentTimeMillis()); String filename = formatter.format(curDate); String strFolderName = Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES) + File.separator + "HONGDROID" + File.separator; File file = new File(strFolderName); if( !file.exists() ) file.mkdirs(); File f = new File(strFolderName + "/" + filename + ".png"); result = f.getPath(); FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); result = "Save Error fOut"; } // 비트맵 사진 폴더 경로에 저장 rotate(bitmap,exifDegree).compress(Bitmap.CompressFormat.PNG, 70, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); // 방금 저장된 사진을 갤러리 폴더 반영 및 최신화 mMediaScanner.mediaScanning(strFolderName + "/" + filename + ".png"); } catch (IOException e) { e.printStackTrace(); result = "File close Error"; } // 이미지 뷰에 비트맵을 set하여 이미지 표현 ((ImageView) findViewById(R.id.iv_result)).setImageBitmap(rotate(bitmap,exifDegree)); } } private int exifOrientationToDegress(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); } PermissionListener permissionListener = new PermissionListener() { @Override public void onPermissionGranted() { Toast.makeText(getApplicationContext(), "권한이 허용됨",Toast.LENGTH_SHORT).show(); } @Override public void onPermissionDenied(ArrayList<String> deniedPermissions) { Toast.makeText(getApplicationContext(), "권한이 거부됨",Toast.LENGTH_SHORT).show(); } }; } | cs |
'Android Java' 카테고리의 다른 글
#13 안드로이드 스튜디오 프래그먼트 (Fragment) 예제 [ 홍드로이드 ] (0) | 2019.06.02 |
---|---|
#12 안드로이드 스튜디오 리사이클러뷰 (Recycler View) 예제 [ 홍드로이드 ] (2) | 2019.05.21 |
#10 안드로이드 스튜디오 네비게이션 메뉴 (Navigation Menu Custom) 예제 [ 홍드로이드 ] (2) | 2019.05.21 |
#9 안드로이드 스튜디오 웹뷰 (WebView) 예제 [ 홍드로이드 ] (3) | 2019.05.21 |
#8 안드로이드 스튜디오 데이터 저장 ( Shared Preferences ) 예제 [ 홍드로이드 ] (0) | 2019.05.21 |
Comments