메뉴 건너뛰기

조회 수 7668 추천 수 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.}

  1. 알아놓으면 좋은 내용정리

    Date2016.06.07 Views7466
    Read More
  2. 암시적 인텐트를 사용한 인터넷열기, 전화걸기, 문자보내기 [Intent (인텐트)]

    Date2016.06.07 Views7756
    Read More
  3. 앱 번들(Android App Bundle) 만들기

    Date2021.09.14 Views309
    Read More
  4. 어댑터 뷰(Adapter View) & 어댑터(Adapter) (1)

    Date2016.06.08 Views7864
    Read More
  5. 옵션 메뉴 동적으로 생성하기

    Date2015.07.16 Views6933
    Read More
  6. 월별 캘린더에 일정 입력 및 조회 기능 리스트로 추가하기

    Date2015.07.16 Views18561
    Read More
  7. 위젯 업데이트 주기 빠르게 하기

    Date2018.10.02 Views2168
    Read More
  8. 이미지 버튼 설정

    Date2015.07.16 Views6384
    Read More
  9. 이미지 버튼(ImageButton) 만들기

    Date2015.07.16 Views7116
    Read More
  10. 이미지의 Orientation를 체크해서 이미지 회전하기

    Date2015.07.16 Views7668
    Read More
  11. 인텐트를 이용한 Activity간 데이터 전달 (사용자 정의 클래스)

    Date2015.07.16 Views7067
    Read More
  12. 전화 인텐트와 나의 전화 번호가져오기

    Date2014.08.28 Views6322
    Read More
  13. 줄바꿈 문자 치환

    Date2020.12.14 Views299
    Read More
  14. 체크 박스(CheckBox)의 이미지 바꾸기

    Date2015.07.16 Views6419
    Read More
  15. 초기화면 페이지를 만들어보자. splash 페이지 제작

    Date2020.12.14 Views290
    Read More
  16. 카카오톡 대화내용 가져오기(sqlite3, chat_logs)

    Date2016.05.26 Views15170
    Read More
  17. 카카오톡 분석하기 (1) - sqlite 파해치기

    Date2016.05.26 Views10464
    Read More
  18. 카카오톡 분석하기 (2) - 카카오톡 암호화 함수 찾기

    Date2016.05.26 Views9612
    Read More
  19. 클래스나눠서 xml 파싱과 FTP를이용하여 안드로이드에서 활용하기

    Date2014.08.28 Views6187
    Read More
  20. 탭 뷰에 탭 추가하기, 아이콘 넣기

    Date2015.07.16 Views9365
    Read More
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 Next
/ 13

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved