Please Enable JavaScript!
Gon[ Enable JavaScript ]

안드로이드 이미지 회전과 축소 기능 구현

안드로이드 개발
반응형

Android 이미지 회전과 축소 기능 구현

 

개발환경 : JDK 1.6, Android 2.1, window XP

 

안드로이드에서 이미지 프로세싱을 하기 위해서는 제약이 따른다.

휴대폰으로 찍은 그림이나 sdcard 에 넣어둔 그림의 사이즈가

1메가 이상이면 Out of memory  에러가 발생한다. 스마트폰 특성상

어쩔수 없는 제약사항이며 고화질의 이미지를 작은 화면에서

크게 보여줄 이유가 없다.

 

이미지 프로세싱에 관련된 여러가지 기능이 있지만 여기에선

회전과 View 위젯 사이즈에 맞게 이미지 크기를 조절하는

방법에 관한 간단한 소스 이다.

기능은 아래 버튼을 클릭할 때 마다 90 도 씩 회전하면서

화면에 맞추면서 돌아간다. 이미지 해상도를 줄이는 방법은 여러가지 이다.

 

Matrix 클래스를 이용하는 방법

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

matrix.postScale(scaleWidth, scaleHeight);

 

BitmapFactory.Options 를 이용해 2의 배수로 줄이는 방법

BitmapFactory.Options options = new BitmapFactory.Options();

options.inSampleSize = 4;

Bitmap orgImage = BitmapFactory.decodeFile(“/sdcard/test.jpg”, options);

Bitmap resize = Bitmap.createScaledBitmap(orgImage, 300, 400, true)

 

해상도는 위와 같이 줄이고 회전시키는 방법은 Matrix 클래스의 postRotate함수를

이용했다.

Matrix matrix = new Matrix();

matrix.postRotate(nRotate); // 회전

 

// 이미지를 회전시킨다

Bitmap rotateBitmap =

Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);

 

실행화면



전체소스

 

public class Main extends Activity implements OnClickListener{

       

        private ImageView mImageView;

        private Button mBtnRotate;

        private Bitmap mOrgImage;

        private int currentAngle = 0;

       

        // 한번에 회전해야 되는 각도

        private static final int ROTATE_VALUE = 90;

       

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        // 예제로 쓰일 비트맵 이미지

        mOrgImage = BitmapFactory.decodeResource(getResources(), R.drawable.test);

       

        mImageView = (ImageView)findViewById(R.id.img);

        mImageView.setImageBitmap(mOrgImage);

       

        mBtnRotate = (Button)findViewById(R.id.btn_rotate);

        mBtnRotate.setOnClickListener(this);

    }

 

        /* (non-Javadoc)

         * @see android.view.View.OnClickListener#onClick(android.view.View)

         */

        public void onClick(View v) {

              

               switch (v.getId()) {

               case R.id.btn_rotate:

                       int width = mImageView.getWidth();

                int height = mImageView.getHeight();

                currentAngle = ROTATE_VALUE + (currentAngle % 360);

                       Bitmap resize = getImageProcess(mOrgImage, currentAngle, width, height);

                       mImageView.setImageBitmap(resize);

                       break;

 

               default:

                       break;

               }

        }

       

        /**

         * 이미지 관련 처리를 수행한다.

         * 회전을 시키며, 이미지를 화면에 맞게 줄인다.

         * @author master

         * 2011. 3. 31.

         */

        private Bitmap getImageProcess(Bitmap bmp, int nRotate, int viewW, int viewH){

       

        Matrix matrix = new Matrix();

       

        // 이미지의 해상도를 줄인다.

        /*float scaleWidth = ((float) newWidth) / width;

        float scaleHeight = ((float) newHeight) / height;

        matrix.postScale(scaleWidth, scaleHeight);*/

       

        matrix.postRotate(nRotate); // 회전

        // 이미지를 회전시킨다

        Bitmap rotateBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);

       

        // View 사이즈에 맞게 이미지를 조절한다.

        Bitmap resize = Bitmap.createScaledBitmap(rotateBitmap, viewW, viewH, true);

       

        return resize;

        }

}

Xml 파일 설정

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

          

       <ImageView android:id="@+id/img"

        android:layout_width="fill_parent"

        android:layout_height="0dip"

        android:layout_weight="13"

           android:scaleType="fitXY"/>

          

       <LinearLayout

           android:layout_width="fill_parent"

           android:layout_height="0dip"

           android:layout_weight="2"

           android:layout_gravity="bottom"

           android:paddingTop="8dip"

           android:gravity="center_vertical"       

           android:orientation="horizontal">

             <Button android:id="@+id/btn_rotate"

                    android:layout_width="0dip"

                    android:layout_weight="1"

                 android:layout_height="50dip"

                 android:paddingTop="5dip"

                 android:text="90 회전 "/>

       </LinearLayout>

</LinearLayout>

반응형
Posted by 녹두장군1
,