嘿,大家好我用下面的代碼從我的應用程序訪問攝像頭。 該應用程序能夠訪問攝像頭我也有增加了一個按鈕,其onclicklistener增加了這行代碼: -如何捕獲圖像,並將其保存在SD卡
camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);
現在,我不知道會發生什麼,但應用程序卡住我必須強制關閉,然後我甚至無法訪問本機相機應用程序。 我認爲它會在不釋放Camera對象的情況下離開應用程序。 再加上圖像永遠不會被保存。
代碼: -
SurfaceHolder holder;
SurfaceView surface;
Camera camera;
Boolean isPreviewRunning, fromOnResume;
//Preview mpreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isPreviewRunning = false;
fromOnResume = false;
// requestWindowFeature(Window.FEATURE_NO_TITLE);
surface = (SurfaceView)findViewById(R.id.surface);
holder = surface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button btn = (Button)findViewById(R.id.click);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);
//onCreate(null);
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
/* if (isPreviewRunning) {
camera.stopPreview();
} */
Camera.Parameters parameters = camera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size size = sizes.get(0);
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
camera.startPreview();
isPreviewRunning=true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera=null;
}
Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
camera.startPreview();
}
};
Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera c) {
Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
camera.startPreview();
}
};
Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
public void onShutter() {
Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
}
};
/*
* protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
}
protected void onResume()
{
Log.e(getClass().getSimpleName(), "onResume");
camera.open();
fromOnResume=true;
super.onResume();
}
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
camera.release();
super.onPause();
}
protected void onStop()
{
Log.e(getClass().getSimpleName(), "onStop");
super.onStop();
}*/
GENUIN ...它解決了這個目的,非常感謝......我的下一步將是將圖片上傳到服務器...加上我想我應該有圖像的名稱根據日期和時間ISN那是一個不錯的選擇?原因目前所有的圖像都被保存在我的名字只:) – abhishek 2011-04-14 10:21:55
你應該[查看該項目(https://github.com/thibault/UrbanJungle):有一個Android應用程序捕獲和照片上傳到遠程服務器。該服務器的代碼也被嵌入。 – 2011-04-14 11:50:28