Anroid实现照相代码

本文详细介绍了如何在Android应用中实现拍照功能,包括调用相机API、处理拍照后的图片数据以及在应用中显示照片的完整步骤,助你轻松集成这一常用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class MyCamera extends Activity  implements Callback 
{
	private SurfaceView m_surfaceView = null;
	private SurfaceHolder m_surfaceFolder = null;
	private Button m_btnCamera = null;
	private Button m_btnExit = null;
	private Camera m_Camera;
	private int m_iWidth = 600;
	private int m_iheight = 360;
	private String m_sFilePath = "";
	private String m_sFileName = "";	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        
        m_surfaceView = (SurfaceView)findViewById(R.id.sfvCamera);
        m_btnCamera = (Button)findViewById(R.id.btnCamera);
        m_btnExit = (Button)findViewById(R.id.button1);
        
        //Take picture
        m_btnCamera.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v) 
			{
				m_Camera.autoFocus(m_AutoFocusCallBack);
			}        	
        });
        
        //Get holder according to SurfaceView
        m_surfaceFolder = m_surfaceView.getHolder();
        m_surfaceFolder.addCallback(this);
        m_surfaceFolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
        
        //Set the storage path of picture 
        this.setPicPath();        
    }

	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 
	{
		Camera.Parameters parameters = m_Camera.getParameters();
		
		//Set the format of the picture
		parameters.setPictureFormat(PixelFormat.JPEG);

		Size size = parameters.getPreviewSize();
		if (size.width != 640 && size.height != 480)
		{
			parameters.setPreviewSize(m_iWidth, m_iheight);// set the size
		}
		
		m_Camera.setParameters(parameters);
		try 
		{
			m_Camera.setPreviewDisplay(arg0);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			m_Camera.release(); 
			m_Camera = null; 
		}
		m_Camera.startPreview();
	}
    
	public void surfaceCreated(SurfaceHolder arg0) 
	{
		//Open camera
		m_Camera = Camera.open();
	}

	public void surfaceDestroyed(SurfaceHolder arg0) 
	{
		//Stop taking picture
		m_Camera.stopPreview();
		
		//Release resource
		m_Camera.release();
		m_Camera = null;
	}
	
	private AutoFocusCallback m_AutoFocusCallBack = new AutoFocusCallback() 
	{
		public void onAutoFocus(boolean success, Camera camera) 
		{
			m_Camera.takePicture(null, null, null, m_PictureCallback);
		}
	};
	
	PictureCallback m_PictureCallback = new PictureCallback() 
	{
		public void onPictureTaken(byte[] data, Camera camera) 
		{
			if (data != null) 
			{	
				//Save bitmap into SDCard
				savePictureInSDCard(data);
			}
		}
	};
	
	private boolean setPicPath() 
	{
		boolean bRet = true;
		File fileDir = null; 
		
		//Judge whether SdCard exists
		boolean bSdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 
		if (bSdCardExist) //Exist
		{ 
			//Get the root path
			fileDir = Environment.getExternalStorageDirectory(); 
		} 
		else
		{
			return false;
		}
		
		//Set the storage path of picture
		m_sFilePath = fileDir.toString() + "/ajis/pic/";
		
		//Create path
		File file = new File(m_sFilePath);
		if (!file.exists()) 
		{
			file.mkdirs();
		}
		return bRet;
	}
	
	private void savePictureInSDCard(byte[] bytes)
	{
		//File path and name
		m_sFileName= DateFormat.format(getResources()
				.getString(R.string.strFormat_yyyyMMddhhMMss), Calendar
				.getInstance(Locale.CHINA))
				+ ".jpg";
		
		//Create file
		File file = new File(m_sFilePath + m_sFileName);				
		if (!file.exists()) 
		{
			try 
			{
				file.createNewFile();
			}					
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
		FileOutputStream out = null; 
		try 
		{
			out = new FileOutputStream(m_sFilePath + m_sFileName);
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		}
		try 
		{
			out.write(bytes);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		try 
		{
			out.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}		
	}
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值