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();
}
}
}