메뉴 건너뛰기

조회 수 7658 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

안드로이드에서 단말기 내부의 사진을 ImageView 등에 출력할 때, 
이미지가 가로 방향인지 세로 방향인지 체크해서 적절하게 회전시켜서 보여주는 함수입니다.


이미지의 Orientation 정보를 얻는 함수입니다.

01.public synchronized static int GetExifOrientation(String filepath)
02.{
03.    int degree = 0;
04.    ExifInterface exif = null;
05.     
06.    try
07.    {
08.        exif = new ExifInterface(filepath);
09.    }
10.    catch (IOException e)
11.    {
12.        Log.e(TAG, "cannot read exif");
13.        e.printStackTrace();
14.    }
15.     
16.    if (exif != null)
17.    {
18.        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
19.         
20.        if (orientation != -1)
21.        {
22.            // We only recognize a subset of orientation tag values.
23.            switch(orientation)
24.            {
25.                case ExifInterface.ORIENTATION_ROTATE_90:
26.                    degree = 90;
27.                    break;
28.                     
29.                case ExifInterface.ORIENTATION_ROTATE_180:
30.                    degree = 180;
31.                    break;
32.                     
33.                case ExifInterface.ORIENTATION_ROTATE_270:
34.                    degree = 270;
35.                    break;
36.            }
37. 
38.        }
39.    }
40.     
41.    return degree;
42.}



이미지를 특정 각도로 회전하는 함수입니다.

01.public synchronized static Bitmap GetRotatedBitmap(Bitmap bitmap, int degrees)
02.{
03.    if ( degrees != 0 && bitmap != null )
04.    {
05.        Matrix m = new Matrix();
06.        m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2 );
07.        try
08.        {
09.            Bitmap b2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
10.            if (bitmap != b2)
11.            {
12.                bitmap.recycle();
13.                bitmap = b2;
14.            }
15.        }
16.        catch (OutOfMemoryError ex)
17.        {
18.            // We have no memory to rotate. Return the original bitmap.
19.        }
20.    }
21.     
22.    return bitmap;
23.}



이미지를 불러오는 함수입니다.

01.public synchronized static Bitmap SafeDecodeBitmapFile(String strFilePath)
02.{
03.    DEBUG.SHOW_DEBUG(TAG, "[ImageDownloader] SafeDecodeBitmapFile : " + strFilePath);
04.     
05.    try
06.    {
07.        File file = new File(strFilePath);
08.        if (file.exists() == false)
09.        {
10.            DEBUG.SHOW_ERROR(TAG, "[ImageDownloader] SafeDecodeBitmapFile : File does not exist !!");
11.             
12.            return null;
13.        }
14.         
15.        // Max image size
16.        final int IMAGE_MAX_SIZE    = GlobalConstants.getMaxImagePixelSize();   
17.        BitmapFactory.Options bfo   = new BitmapFactory.Options();
18.        bfo.inJustDecodeBounds      = true;
19.         
20.        BitmapFactory.decodeFile(strFilePath, bfo);
21.         
22.        if(bfo.outHeight * bfo.outWidth >= IMAGE_MAX_SIZE * IMAGE_MAX_SIZE)
23.        {
24.            bfo.inSampleSize = (int)Math.pow(2, (int)Math.round(Math.log(IMAGE_MAX_SIZE
25.                                / (double) Math.max(bfo.outHeight, bfo.outWidth)) / Math.log(0.5)));
26.        }
27.        bfo.inJustDecodeBounds = false;
28.        bfo.inPurgeable = true;
29.        bfo.inDither = true;
30.         
31.        final Bitmap bitmap = BitmapFactory.decodeFile(strFilePath, bfo);
32.         
33.        int degree = GetExifOrientation(strFilePath);
34.         
35.        return GetRotatedBitmap(bitmap, degree);
36.    }
37.    catch(OutOfMemoryError ex)
38.    {
39.        ex.printStackTrace();
40.         
41.        return null;
42.    }
43.}

List of Articles
번호 제목 날짜 조회 수
177 안드로이드 - switch를 사용법 및 구현 file 2021.04.02 1280
176 [하이브리드앱] userAgent를 이용해서 웹 / 앱 접속 구분하기 2021.09.30 1282
175 안드로이드 - 타이머(Timer) 구현하기 2021.04.01 1368
174 Volley 로 웹요청하고 응답받기2 - Post방식 , 로그인-회원가입 (php,mysql 연동) file 2020.12.14 1430
173 안드로이드 - BottomNavigationView 사용하여 하단 메뉴 만들기 file 2021.04.02 1432
172 Apk manager 이용해 Decompile (디컴파일) 하기 file 2021.03.16 1619
171 안드로이드 WebView 에서 tel: 이 되지않는 경우. 2018.10.02 1632
170 안드로이드 - 날짜 및 시간 정보 입력받기 (DatePickerDialog / TimePickerDialog) file 2021.04.01 1762
169 Android Studio에서 SQLCipher 라이브러리 추가 방법 file 2018.10.02 1776
168 MediaPlayer 클래스 사용법 file 2018.10.02 1803
167 안드로이드 - 네비게이션 드로어(Navigation Drawer)를 활용하여 슬라이드 메뉴 구현하기 file 2021.04.01 1858
166 위젯 업데이트 주기 빠르게 하기 2018.10.02 2142
165 구글맵으로 GPS 현재위치 실시간 연동하기 file 2020.12.14 2440
164 WebView에서 카메라 및 이미지 업로드 (선택적용가능) file 2020.12.14 2690
163 안드로이드 - 에디트텍스트(EditText) 사용법 정리 file 2021.03.29 2752
162 WebView를 사용할때 HttpClient를 이용한 Session 유지 2018.12.27 4380
161 하이브리드 앱에서의 세션관리(로그인 상태 유지) 2018.12.27 4998
160 HTML5 Web Storage -01- file 2014.09.04 5587
159 HTML5 시작하기 file 2014.09.04 5660
158 Events - Unbind() 메서드 (이벤트 처리기 해제) file 2014.10.16 5749
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved