2011-04-14 160 views
1

嘿,大家好我用下面的代碼從我的應用程序訪問攝像頭。 該應用程序能夠訪問攝像頭我也有增加了一個按鈕,其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(); 
}*/ 

回答

9

我知道這是不完全的回答你的問題,但它wouldn'nt更容易使用股票相機應用程序?您可以在活動使用此代碼訪問它:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.capture); 

    Button capture = (Button) findViewById(R.id.capture_button); 
    capture.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      // We use the stock camera app to take a photo 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri()); 
      startActivityForResult(intent, TAKE_PHOTO_CODE); 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { 
     Uri imagePath = getImageUri(); 

     doSomething(); 
    } 
} 

/** 
* Get the uri of the captured file 
* @return A Uri which path is the path of an image file, stored on the dcim folder 
*/ 
private Uri getImageUri() { 
    // Store image in dcim 
    File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", CAPTURE_TITLE); 
    Uri imgUri = Uri.fromFile(file); 

    return imgUri; 
} 
+0

GENUIN ...它解決了這個目的,非常感謝......我的下一步將是將圖片上傳到服務器...加上我想我應該有圖像的名稱根據日期和時間ISN那是一個不錯的選擇?原因目前所有的圖像都被保存在我的名字只:) – abhishek 2011-04-14 10:21:55

+0

你應該[查看該項目(https://github.com/thibault/UrbanJungle):有一個Android應用程序捕獲和照片上傳到遠程服務器。該服務器的代碼也被嵌入。 – 2011-04-14 11:50:28

0

只是一個建議,使用EXIF上JPG上傳到服務器之前。由於今天的智能手機相機的質量,我發現上傳圖片的速度很慢。一個簡單的解決方案是使用一個EXIF讀取程序以提取JPEG的縮略圖,所述縮略圖保存爲新的JPEG和上傳。這是等同於原始照片卻小得多(下100KB)。我不知道這是否是你想要的圖像質量,但如果沒有,並上傳大量的圖片,請使用EXIF方法。我在python sl4a上編程並使用EXIF.py,但我確定Java中有類似的東西。

相關問題