¾È±Ô °øºÎ¹æ

Android > SurfaceView - Ä«¸Þ¶ó ÇÁ¸®ºä ¶ç¿ì±â

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

SurfaceView?? ?Ç¥¸éÀÇ ºä??

SurfaceView´Â TextView³ª ImageView ¿Í °°ÀÌ À§Á¬¸¦ Ç¥½ÃÇÒ ¼ö ÀÖ´Â ViewÀÇ ÀÏÁ¾ÀÔ´Ï´Ù.

ÇÏÁö¸¸ ÀÌ SurfaceVIew¶ó´Â À̸§¿¡¼­ ¾Ë ¼ö ÀÖµíÀÌ  ´Ù¸¥ VIewµé°ú´Â ´Þ¸® Á÷Á¢ SurfaceView°¡ À§Á¬¸¦ Ç¥½ÃÇÏÁö ¾Ê½À´Ï´Ù.

´Ù¸¥ View´Â È­¸é¿¡ ºä¸¦ Ç¥½ÃÇÏ´Â °úÁ¤°ú »ç¿ëÀÚ¿ÍÀÇ »óÈ£ÀÛ¿ë ó¸® µîÀÌ ¸ðµÎ ÇϳªÀÇ ¾²·¹µå(¸ÞÀÎ)¿¡¼­ 󸮵˴ϴÙ.

ÀÌ·¯ÇÑ Æ¯¼ºÀ¸·Î ÀÎÇØ ANRÀÌ ¹ß»ýÇÕ´Ï´Ù.

Áï, ANRÀº ¾îÇø®ÄÉÀ̼ÇÀÌ 5ÃÊ ÀÌ»ó µ¿ÀÛÀ» ¸ØÃèÀ» ¶§ ¹ß»ýÇϴµ¥ ÀÌ´Â °°Àº ¾²·¹µå ³»¿¡¼­ 󸮵DZ⠶§¹®¿¡ ½Ã°£ÀÌ Áö¿¬µÇ¾î ¹ß»ýÇÏ´Â °ÍÀÔ´Ï´Ù.

±×·±µ¥ Ä«¸Þ¶ó¸¦ »ç¿ëÇϱâ À§Çؼ­´Â °í¼º´É - °íÈ­ÁúÀÇ ÇÁ·¹ÀÓÀÌ ¾öû³ª°Ô ºü¸¥ ¼Óµµ·Î È­¸éÀ» ¾÷µ¥ÀÌÆ®¸¦ ÇÏ¿© »ç¶÷ÀÇ ´«¿¡´Â

Ä«¸Þ¶óºä°¡ º¸ÀÌ´Â °ÍÀÔ´Ï´Ù.

ÀÌ·± ¸¹Àº ÀÛ¾÷À» Çϴµ¥ ÀÏ¹Ý ºä¸¦ »ç¿ëÇÑ´Ù¸é ºÐ¸í ¾²·¹µå¿¡ °úºÎÇÏ°¡ °É·Á¼­ ANRÀÌ ¹ß»ýÇÒ °ÍÀÔ´Ï´Ù.

±×·¸±â ¶§¹®¿¡ SurfaceView¸¦ »ç¿ëÇÏ´Â °ÍÀÔ´Ï´Ù.

ÀÌ SurfaceView´Â È­¸é ¾÷µ¥ÀÌÆ®¸¦ ¹é±×¶ó¿îµå ¾²·¹µå·Î ¼öÇàÇÏ¿© ¿øÈ°ÇÏ°Ô ºä¸¦ ¾÷µ¥ÀÌÆ®ÇØÁÝ´Ï´Ù.

À̹ÌÁöºä³ª ÅؽºÆ®ºä´Â ºä ÀÚü°¡ ¹Ù·Î ÄÁÅÙÃ÷¸¦ Ç¥½ÃÇÏÁö¸¸,SurfaceView´Â Á¶±Ý ƯÀÌÇÕ´Ï´Ù.

SurfaceView´Â ´Ü¼øÈ÷ Ʋ ¿ªÇÒÀ» ÇÏ°í , ÄÁÅØÃ÷°¡ Ç¥ÇöµÇ´Â °÷Àº SurfaceView ³»¿¡ Surface °´Ã¼ÀÔ´Ï´Ù.

À§ ±×¸²¿¡¼­ º¸µí SurfaceHolder Ŭ·¡½º°¡ ½ÇÁ¦·Î Surface¿¡ Á¢±ÙÇÏ¿© È­¸éÀ» ó¸®ÇØÁÖ´Â ±¸Á¶¸¦ °¡Áö°í ÀÖ½À´Ï´Ù.

SurfaceHolder¶ó´Â À̸§ ±×´ë·Î Surface °´Ã¼¸¦ Àâ°í(Hold) °ü¸®ÇØ ÁÖ´Â °ÍÀ¸·Î »ý°¢ÇÏ¸é µÉ °Í °°½À´Ï´Ù.
 
	class Preview extends SurfaceView implements SurfaceHolder.Callback {
		SurfaceHolder mHolder;
		
		Preview(Context context) {
			super(context);
			
			mHolder = getHolder();		
			mHolder.addCallback(this);
			mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	    }

		public void surfaceCreated(SurfaceHolder holder) {       

		}

		public void surfaceDestroyed(SurfaceHolder holder) {       

		}

		public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		
		}
	}
	


À§¿¡¼­ ¸»ÇßµíÀÌ SurfaceView¸¦ »ç¿ëÇϱâ À§Çؼ­´Â SurfaceView°´Ã¼¸¦ Á÷Á¢ »ç¿ëÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó SurfaceView¸¦ »ó¼ÓÇÑ´Ù.

¶ÇÇÑ SurfaceHolder.CallbackÀ» ±¸ÇöÇϴ Ŭ·¡½º¸¦ »ý¼ºÇØ¾ß ÇÑ´Ù.

SurfaceHolder mHolder;

Ŭ·¡½ºÀÇ ¸â¹ö·Î SurfaceHolder °´Ã¼ÀÎ mHolder °´Ã¼¸¦ ¼±¾ð. ÀÌ °´Ã¼¸¦ ÅëÇØ ½ÇÁ¦ ÄÁÅÙÃ÷°¡ Ç¥½ÃµÇ´Â ¿µ¿ªÀÎ Surface¸¦ °ü¸®ÇÕ´Ï´Ù.

mHolder = getHolder();

getHolder() ¸Þ¼Òµå¸¦ ÅëÇØ ÇöÀç SurfaceViewÀÇ SurfaceHolder ÀνºÅϽº¸¦ ¿¬°áÇØÁÖ°í ÀÖ´Â °ÍÀ» È®ÀÎÇÒ ¼ö ÀÖ½À´Ï´Ù.

À̷νá mHolder °´Ã¼¸¦ ÅëÇØ ÀÌ SurfaceViewÀÇ Surface¿¡ Á¢±ÙÇÒ ¼ö ÀÖ°Ô µÇ¾ú½À´Ï´Ù.


¾ÕÀÇ ±Û¿¡¼­ SurfaceView´Â SurfaceView ÀÚü°¡ ³»¿ëÀ» Ç¥½ÃÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó ³»ºÎÀÇ SurfaceHolder¸¦ ÅëÇØ ÃÖÁ¾ÀûÀ¸·Î´Â Surface¶ó´Â °´Ã¼ À§¿¡

±× ³»¿ëÀ» Ç¥½ÃÇÑ´Ù°í ÇÏ¿´´Âµ¥, À§ÀÇ °æ¿ì´Â ¾Æ·¡¿Í °°ÀÌ Ä«¸Þ¶óÀÇ ¿µ»óÀÌ Ã³¸®µÈ´Ù°í º¼ ¼ö ÀÖ½À´Ï´Ù.



±×·³, Ä«¸Þ¶ó·Î ¹ÞÀº ¿µ»óÀ» ÇÁ¸®ºä·Î »Ñ¸®´Â ¿¹Á¦¸¦ Çѹø ¸¸µé¾î º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
 
	package com.example.study;

	import android.app.Activity;
	import android.graphics.Bitmap;
	import android.graphics.BitmapFactory;
	import android.hardware.Camera;
	import android.os.Bundle;
	import android.util.Log;
	import android.view.SurfaceHolder;
	import android.view.SurfaceView;
	import android.view.View;
	import android.view.Window;
	import android.widget.Button;
	import android.widget.ImageView;

	public class CameraTest extends Activity {

		protected static final String TAG = null;
		private SurfaceView surfaceView;	  
		private SurfaceHolder surfaceHolder;
		private Camera camera;
		private ImageView imageview;
		private boolean inProgress;

		
		
		// 2. ±×´ÙÀ½ ½ÃÀÛÇÏ´Â°Ô onCreate. Activity »ý¼º½Ã Àê ¸ÕÀú ½ÇÇàµÈ´Ù.
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
		
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			
			setContentView(R.layout.cameratest);
			
			imageview = (ImageView)findViewById(R.id.imageView1);
			surfaceView =  (SurfaceView) findViewById(R.id.surfaceView1);
			
			surfaceHolder = surfaceView.getHolder();
			surfaceHolder.addCallback(surfaceListener);
			//surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

			findViewById(R.id.button1).setOnClickListener(
				new Button.OnClickListener() {
					@Override
					public void onClick(View v) {

						if(camera !=null && inProgress == false)
						{
							camera.takePicture(null, null, takePicture);
							inProgress = true;
						}
					}
				}
			);			
		}


		private Camera.PictureCallback takePicture = new Camera.PictureCallback() {
			
			@Override
			public void onPictureTaken(byte[] data, Camera camera) {
				// TODO Auto-generated method stub
				Log.i(TAG, "¼¦´Ù ´©¸§ È®ÀÎ");
				
				if(data!=null)
					Log.i(TAG, "JPEG »çÁø Âï¾úÀ½!");				
					Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);					
					imageview.setImageBitmap(bitmap);					
					camera.startPreview();					
					inProgress = false;
					
					
				
			}
		};
		
		private SurfaceHolder.Callback surfaceListener = new SurfaceHolder.Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				camera.release();
				camera = null;
				Log.i(TAG, "Ä«¸Þ¶ó ±â´É ÇØÁ¦");
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				camera = Camera.open();
				Log.i(TAG, "Ä«¸Þ¶ó ¹Ì¸®º¸±â È°¼º");
				
				try {
					camera.setPreviewDisplay(holder);
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
				// TODO Auto-generated method stub
				Camera.Parameters parameters = camera.getParameters();
				parameters.setPreviewSize(width, height);
				camera.startPreview();
				Log.i(TAG,"Ä«¸Þ¶ó ¹Ì¸®º¸±â È°¼º");
				
			}
		};
	}
	


ÈÞ´ëÆù ±ÇÇÑÀº..... ¾Æ·¡Ã³·³...
	<uses-feature android:name="android.hardware.camera"/>
	<uses-permission android:name="android.permission.CAMERA"/>

 

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