¾È±Ô °øºÎ¹æ

Android > ÃÔ¿µÈÄ À̹ÌÁö ÀúÀå, Crop ÈÄ Imageview ¿¡ ¶ç¿ì±â

µî·ÏÀÏ : 2017-07-06 17:54 Á¶È¸¼ö : 53,096

»çÁøÃÔ¿µÈÄ Image¸¦ Crop Çϸç Pick ÇÏ´Â ¹æ¹ý¿¡ ´ëÇÑ Sample Code ¸¦ Á¦°øÇÑ´Ù.

¼³¸íÀº ÁÖ¼®À» Âü°íÇϱ⠹ٶø´Ï´Ù.
 
	public class WriteActivity  extends Activity implements OnClickListener{
		private static final int PICK_FROM_CAMERA = 0;  //Ä«¸Þ¶ó ÃÔ¿µ
		private static final int PICK_FROM_ALBUM = 1;	//¾Ù¹ü ¼±ÅÃ
		private static final int CROP_FROM_CAMERA = 2;	//Ä«¸Þ¶ó ÃÔ¿µÈÄ Å©·Ó

		private static final String TYPE_IMAGE = "image/*";

		private static final int PROFILE_IMAGE_ASPECT_X = 1;
		private static final int PROFILE_IMAGE_ASPECT_Y = 1;
		private static final int PROFILE_IMAGE_OUTPUT_X = 600;
		private static final int PROFILE_IMAGE_OUTPUT_Y = 600;
		private static final String TEMP_FILE_NAME = "tempFile.jpg";

		private Uri mImageCaptureUri;
		private ImageView mPhotoImageView;
		private byte[] imgbyte;
		private EditText mEdityEntry; 

		private Uri mTempImageUri;

		protected void onCreate(Bundle savedInstanceState){
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_wirte);

			Button btn = (Button) findViewById(R.id.btn_send);
			btn.setOnClickListener(this);

			Button album_btn = (Button) findViewById(R.id.btn_album);
			album_btn.setOnClickListener(this);

			Button camera_btn = (Button) findViewById(R.id.btn_camera);
			camera_btn.setOnClickListener(this);

			mPhotoImageView = (ImageView) findViewById(R.id.img_imageview);
			
			mEdityEntry = (EditText)findViewById(R.id.edit_id);
		}
		
		public void onClick(View v) {
			if (v.getId() == R.id.btn_send) {
				try {
					
					String mFilePath="";
					//DoFileUpload(mFilePath);
				} catch (Exception e) {
				}
				
			} else if (v.getId() == R.id.btn_album) {
				doAlbumCrop();
			} else if (v.getId() == R.id.btn_camera) {
				Intent intent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE, null);
				startActivityForResult( intent, PICK_FROM_CAMERA );
			}

		}
		
		private File getTempFile(){	
			File file = new File( Environment.getExternalStorageDirectory(), TEMP_FILE_NAME );	
			try{
				file.createNewFile();	
			}	
			catch( Exception e ){	
				Log.e("cklee", "fileCreation fail" );	
			}	
			return file;	
		}

		
		private Uri getJustTakenPictureUri(){
			Cursor cursor = getContentResolver().query( Images.Media.EXTERNAL_CONTENT_URI, 
					new String[]{ Images.ImageColumns.DATA }, null, null, null );	
			if ( cursor == null ) return null;
			String fileName = null;
			if ( cursor.moveToLast())	
				fileName = cursor.getString(0 );
			cursor.close();
			if ( TextUtils.isEmpty( fileName ) ) return null;
			return Uri.fromFile( new File( fileName ) );
		}


		private void doAlbumCrop(){
			mTempImageUri = Uri.fromFile( getTempFile() );
			Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
			intent.setType( TYPE_IMAGE );
			intent.putExtra( "crop", "true" );
			intent.putExtra( "aspectX", PROFILE_IMAGE_ASPECT_X );
			intent.putExtra( "aspectY", PROFILE_IMAGE_ASPECT_Y );
			intent.putExtra( "outputX", PROFILE_IMAGE_OUTPUT_X );
			intent.putExtra( "outputY", PROFILE_IMAGE_OUTPUT_Y);
			intent.putExtra( "scale", true );
			intent.putExtra( MediaStore.EXTRA_OUTPUT, mTempImageUri );
			intent.putExtra( "outputFormat", Bitmap.CompressFormat.JPEG.toString() );
			startActivityForResult( intent, PICK_FROM_ALBUM );
		}
		
		private void doCameraCrop(){
			Uri justTakenPictureUri = getJustTakenPictureUri(); 
		
			mTempImageUri = Uri.fromFile( getTempFile() );
			Intent intent = new Intent( "com.android.camera.action.CROP" );
		
			intent.setDataAndType( justTakenPictureUri, TYPE_IMAGE );
			intent.putExtra( "scale", true );
			intent.putExtra( "aspectX", PROFILE_IMAGE_ASPECT_X );  
			intent.putExtra( "aspectY", PROFILE_IMAGE_ASPECT_Y );  
			intent.putExtra( "outputX", PROFILE_IMAGE_OUTPUT_X);  
			intent.putExtra( "outputY", PROFILE_IMAGE_OUTPUT_Y);
			intent.putExtra( MediaStore.EXTRA_OUTPUT, mTempImageUri );
			startActivityForResult( intent,  CROP_FROM_CAMERA );
		}

		
		private void doImageView(){
			File tempFile = getTempFile();	
			if ( tempFile.exists() )	
				  ((ImageView)findViewById( R.id.img_imageview )).setImageBitmap( BitmapFactory.decodeFile( tempFile.toString()));
		}

		
		@Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
			super.onActivityResult(requestCode, resultCode, data);
		
			switch (requestCode) {	
				case PICK_FROM_CAMERA:{
					doCameraCrop();
					break;
				}
				case PICK_FROM_ALBUM: {	
					doImageView();
				}
				
				case CROP_FROM_CAMERA: {		
					doImageView();
				}
			}	
		}
		
	}


Ä«¸Þ¶ó ¾ÛÀ» ½ÇÇàÇϸ鼭 crop ¿É¼ÇÀ» ÁÖ¸é, ÂïÀ¸¸é¼­ ¹Ù·Î crop µµ °¡´ÉÇÏ´Ù.

ÇÏÁö¸¸, ÀÌ °æ¿ì¿¡´Â return-data ÀÇ ÇüÅ·Πcrop µÈ data °¡ bitmap À¸·Î Àü´ÞµÇ´Âµ¥, bundle ÀÇ ¿ë·®Á¦ÇÑ ¹®Á¦·Î IBinder ¿¡·¯°¡ ³¯ ¼ö ÀÖ´Ù.

Áï Å« »çÀÌÁîÀÇ crop µÈ À̹ÌÁö¿¡´Â Àû¿ëÇϱⰡ ¾î·Æ´Ù. ÀÛÀº »çÀÌÁî¶ó¸é crop ¿É¼ÇÀ» ÁÖ´Â ¹æÇâÀ¸·Î ó¸®ÇÏ´Â °ÍÀÌ ÁÁ´Ù.

ÇÏÁö¸¸!! À̵µ ¹®Á¦°¡ Àִµ¥, Ä«¸Þ¶ó ¾Û¿¡ µû¶ó¼­ crop ¿É¼ÇÀ» ¹«½ÃÇÏ´Â °æ¿ìµµ ÀÖ°í, output ¿É¼ÇÀ» ¹«½ÃÇÏ´Â °æ¿ìµµ Çã´ÙÇÏ´Ù.

Á¦´ë·Î ±¸ÇöµÈ Ä«¸Þ¶ó¾ÛÀÇ °æ¿ì´Â ¸ðµç extra ¿É¼ÇÀ» Ãæ½ÇÈ÷ µû¸£Áö¸¸, ¾î´À Á¤µµ À¯¸íÇÑ ¸ð ȸ»çÀÇ Ä«¸Þ¶ó ¾Û¿¡¼­Á¶Â÷ ÀÌÀü¹öÀü¿¡¼­´Â crop ¿É¼ÇÀ»

¹«½ÃÇÏ°ï Çß´Ù. output ÀÇ °æ¿ì ÇöÀç ´ëºÎºÐÀÇ Ä«¸Þ¶ó°¡ Á¦´ë·Î Áö¿øÀ» ¾È ÇØÁØ´Ù.

ÀÌ·± °æ¿ì¿¡´Â ¹Ýµå½Ã onActivityResult ¸¦ ÅëÇؼ­ crop °úÁ¤À» Çѹø ´õ °ÅÃÄ¾ß ÇÑ´Ù. °á±¹ ¾Èµå·ÎÀ̵å´Â °í·Á»çÇ×ÀÌ Á¤¸» ¸¹´Ù´Â °Í.


Cursor Àú°Ç ¹¹ÇÏ´Â °Å¾ß?

output ¿É¼ÇÀÌ Á¦´ë·Î ¸ÔÀ¸¸é ÁÁÀ¸·Ã¸¸, MediaStore.ACTION_IMAGE_CAPTURE ÀÇ °æ¿ì output ¿É¼ÇÀ» Á¦´ë·Î ¸ÔÁö ¾Ê´Â °æ¿ì°¡ ´ëºÎºÐÀ̾ú´Ù.

ÀÌ·± °æ¿ì¿¡´Â °¡Àå ÃÖ±Ù¿¡ ÂïÈù À̹ÌÁöÀÇ ÁÖ¼Ò¸¦ °¡Á®¿Í¼­ ±× ³à¼®À» crop Çϵµ·Ï À¯µµÇÏ´Â ¹æ¹ýÀ¸·Î ¿ìȸÇؼ­ ±¸ÇöÇÑ °ÍÀÌ´Ù.

ÀÌ ºÎºÐÀº Ä¡¸íÀû °áÇÔÀÌ Àִµ¥, ƯÁ¤»óȲ¿¡¼­ »çÁøÂï´Â °Í°ú À̹ÌÁö ´Ù¿î·Îµå µîÀÇ °úÁ¤ÀÌ µ¿½Ã¿¡ ÀϾ´Ù¸é, ÀÚ½ÅÀÌ ÂïÀº »çÁøÀÌ ¾Æ´Ï¶ó

´Ù¿î·ÎµåÇÑ À̹ÌÁö°¡ crop ´ë»óÀ¸·Î µîÀåÇÒ ¼ö ÀÖ´Ù.

 



Àüü ÇÁ·ÎÁ§Æ® ÆÄÀÏ Ã·ºÎÇÕ´Ï´Ù.

test_join.zip 

 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.