본문 바로가기

안드로이드 개발/개발팁

패스를 통해서 비트map 만들기

패스를 통해서 비트map 만들기

 

 


         String path = m_strSavaImagePath;  // 이미지 경로
        int width = m_ivPhoto.getWidth(); 
        int height = m_ivPhoto.getHeight();
        
        Bitmap bitmap = getBitmapFromPath(path, width, height);

 

 

 

 

 

 public static Bitmap getBitmapFromPath(String path, int targetWidth, int targetHeight) {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(path, bmOptions);
        int photoWidth = bmOptions.outWidth;
        int photoHeight = bmOptions.outHeight;

        int scaleFactor = 1;
        if (targetWidth == 0 || targetHeight == 0) {
            scaleFactor = 2;
        } else {
            scaleFactor = Math.min(photoWidth / targetWidth, photoHeight / targetHeight);
            if (scaleFactor < 2) {
                scaleFactor = 2;
            }
        }
        bmOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inPurgeable = true;

        Bitmap bmp = BitmapFactory.decodeFile(path, bmOptions);
        try {
            ExifInterface exif = new ExifInterface(path);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            int degrees = 0;
            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {   //<==== 이 코드로 이미지 전환 방지
                degrees = 90;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                degrees = 180;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                degrees = 270;
            }

            if (degrees != 0 && bmp != null) {
                Matrix matrix = new Matrix();
                matrix.setRotate(degrees, bmp.getWidth() / 2, bmp.getHeight() / 2);

                try {
                    Bitmap converted = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                    if (bmp != converted) {
                        bmp.recycle();
                        bmp = converted;
                    }
                } catch (OutOfMemoryError e) {
                    ;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmp;
    }