Please Enable JavaScript!
Gon[ Enable JavaScript ]

반응형

안드로이드(Android) 카메라, 갤러리 호출후 이미지 잘래내서 화면에 표시

 

환경 : Eclipse Mars, Android 4.2.2

 

이번예제는 안드로이드에 내장된 카메라와 갤러리 프로그램을 호출해서 이미지를 선택한후 잘라내게 합니다. 잘라낸 이미지를 리턴받아 메인 화면에 표시해 주게 됩니다. 이미지를 잘라내는 옵션을 카메라와 갤러리 프로그램을 Intent 로 호출할 때 적용할수 있습니다.

 

안드로이드(Android) 카메라, 갤러리 호출후 이미지 잘래내서 화면에 표시

 

샘플은 2가지 버튼이 있는데 카메라 호출하는 것과 갤러리 호출입니다. 카메라는 찍고 나서 메인에서 호출할 때 셋팅했던 잘라내기 크기 만큼 이미지를 리턴하게 됩니다. 카메라 호출 하는 소스와 잘라내기 옵션을 지정하기 위한 셋팅값은 다음과 같습니다.

 

// 카메라 호출 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());

// 이미지 잘라내기 위한 크기 
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);

 

이렇게 셋팅한후 startActivityForResult() 함수를 호출합니다. startActivityForResult() 함수를 호출하는 것은 리턴값을 받기 위함입니다. 프로그램을 호출하고 완료하면 onActivityResult() 함수가 호출되면서 결과 값을 받을 수 있는 것이죠.

 

try {
	intent.putExtra("return-data", true);
	startActivityForResult(Intent.createChooser(intent,
	"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
	// Do nothing for now
}

 

startActivityForResult() 호출하였으므로 카메라 프로그램이 호출되어 사진찍고 이미지를 잘라낸후 확인을 누르면 그 결과값이 onActivityResult() 리턴되는데 이곳에 넘어온 Intent 데이터가 결과 값이 되는 것입니다. 카메라에서 넘어온 것이므로 Bitmap 이겠죠.받은 데이터를 화면 ImageView 에 셋팅합니다. onActivityResult() 에서 리턴값으로 넘어오는 requestCode 는 구분자가 되는데 startActivityForResult() 값을 호출할 때 두분째 인수가 됩니다.

 

protected void onActivityResult(int requestCode, 
				int resultCode, 
				Intent data) {
	
	if (requestCode == PICK_FROM_CAMERA) {
		Bundle extras = data.getExtras();
		if (extras != null) {
			Bitmap photo = extras.getParcelable("data");
			imgview.setImageBitmap(photo);
		}
	}
	if (requestCode == PICK_FROM_GALLERY) {
		Bundle extras2 = data.getExtras();
		if (extras2 != null) {
			Bitmap photo = extras2.getParcelable("data");
			imgview.setImageBitmap(photo);
		}
	}
}

 

두번째 버튼인 갤러리에서 가져오기도 카메라에서 가져오기와 별다른 차이는 없습니다. Intent 에 값을 셋팅한후 프로그램을 실행시켜 결과값을 리턴받으면 끝나는 것이죠.

 

buttonGallery.setOnClickListener(new View.OnClickListener() {
	public void onClick(View v) {

		Intent intent = new Intent();
		// Gallery 호출 
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		// 잘라내기 셋팅 
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 0);
		intent.putExtra("aspectY", 0);
		intent.putExtra("outputX", 200);
		intent.putExtra("outputY", 150);
		try {
			intent.putExtra("return-data", true);
			startActivityForResult(Intent.createChooser(intent,
			"Complete action using"), PICK_FROM_GALLERY);
		} catch (ActivityNotFoundException e) {
			// Do nothing for now
		}
	}
});

 

리턴받아서 결과값을 표현하는 onActivityResult() 에서의 소스는 카메라에서와 동일합니다. 에뮬레이터에 카메라가 설치되어있지 않아 카메라에서 가져오기는 해보지 못했지만 갤러리에서 가져오기는 되니 영상으로 참고하시기 바랍니다.

 

 

아래는 메인 activity 의 전체 xml 입니다.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#3F0099"
        android:paddingBottom="@dimen/abc_action_bar_icon_vertical_padding"
        android:paddingTop="@dimen/abc_action_bar_icon_vertical_padding"
        android:text="갤러리, 이미지 프로그램에서 Bitmap 리턴받아 표시하기 "
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/btn_take_camera"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:text="카메라에서 이미지 가져오기"
        android:typeface="sans" />

    <Button
        android:id="@+id/btn_select_gallery"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="갤러리에서 이미지 가져오기 "
        android:typeface="sans" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:scaleType="fitCenter" />

</LinearLayout>

 

아래는 메인 activity 의 전체 소스 입니다.

 

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class CameraGalleryActivity extends Activity{

	private static final int PICK_FROM_CAMERA = 1;
	private static final int PICK_FROM_GALLERY = 2;
	private ImageView imgview;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_camera_gallery);
		imgview = (ImageView) findViewById(R.id.imageView1);
		Button buttonCamera = (Button) findViewById(R.id.btn_take_camera);
		Button buttonGallery = (Button) findViewById(R.id.btn_select_gallery);
		
		buttonCamera.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				// 카메라 호출 
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT,
								MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
				
				// 이미지 잘라내기 위한 크기 
				intent.putExtra("crop", "true");
				intent.putExtra("aspectX", 0);
				intent.putExtra("aspectY", 0);
				intent.putExtra("outputX", 200);
				intent.putExtra("outputY", 150);

				try {
					intent.putExtra("return-data", true);
					startActivityForResult(intent, PICK_FROM_CAMERA);
				} catch (ActivityNotFoundException e) {
					// Do nothing for now
				}
			}
		});

		buttonGallery.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent intent = new Intent();
				// Gallery 호출 
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				// 잘라내기 셋팅 
				intent.putExtra("crop", "true");
				intent.putExtra("aspectX", 0);
				intent.putExtra("aspectY", 0);
				intent.putExtra("outputX", 200);
				intent.putExtra("outputY", 150);
				try {
					intent.putExtra("return-data", true);
					startActivityForResult(Intent.createChooser(intent,
					"Complete action using"), PICK_FROM_GALLERY);
				} catch (ActivityNotFoundException e) {
					// Do nothing for now
				}
			}
		});
	}

	protected void onActivityResult(int requestCode, 
									int resultCode, 
									Intent data) {
		
		if (requestCode == PICK_FROM_CAMERA) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				Bitmap photo = extras.getParcelable("data");
				imgview.setImageBitmap(photo);
			}
		}
		if (requestCode == PICK_FROM_GALLERY) {
			Bundle extras2 = data.getExtras();
			if (extras2 != null) {
				Bitmap photo = extras2.getParcelable("data");
				imgview.setImageBitmap(photo);
			}
		}
	}
}
반응형
Posted by 녹두장군1
,