2012-03-04 248 views
0

我想寫一種相機應用程序,它使用SurfaceView上顯示的相機進行實時預覽,另一層預覽是一個蒙版(有趣的圖片等)。點擊顯示照片的功能。問題是該應用程序在adroid 2.2上工作得很好,但不適用於2.3(使用手機和模擬器)。我有Mainfest中的相機的權利。應用程序適用於android 2.2,但不適用於2.3

這裏是我的activivty:

package funny.camera; 

import android.app.Activity; 
import android.content.pm.ActivityInfo; 
import android.hardware.Camera; 
import android.hardware.Camera.PictureCallback; 
import android.hardware.Camera.ShutterCallback; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 

public class CameraActivity extends Activity{ 

    private CamScreen cam; 
    private LayoutInflater controlInflater = null; 
    SurfaceView surfaceView; 
    SurfaceHolder surfaceHolder; 
    View touchscreen; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     cam = new CamScreen(this); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

     setContentView(R.layout.camscreen); 

     surfaceView = (SurfaceView)findViewById(R.id.camerapreview); 
     surfaceHolder = surfaceView.getHolder(); 
     surfaceHolder.addCallback(cam); 
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  

     controlInflater = LayoutInflater.from(getBaseContext()); 

     View viewControl = controlInflater.inflate(R.layout.camoverlay, null); 
     LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
     addContentView(viewControl, layoutParamsControl); 
     View viewTouch = controlInflater.inflate(R.layout.camtouch, null); 
     addContentView(viewTouch, layoutParamsControl); 

     viewControl.setOnClickListener(new View.OnClickListener() {   
      @Override 
      public void onClick(View v) { 
       cam.camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
      } 
     }); 
    } 
    ShutterCallback shutterCallback = new ShutterCallback(){ 

     @Override 
     public void onShutter() { 

     }}; 

    PictureCallback rawCallback = new PictureCallback(){ 

     @Override 
     public void onPictureTaken(byte[] arg0, Camera arg1) { 

     }}; 

    PictureCallback jpegCallback = new PictureCallback(){ 

     @Override 
     public void onPictureTaken(byte[] _data, Camera _camera) { 

     }}; 
} 

而且classrelated到相機預覽

public class CamScreen extends SurfaceView implements SurfaceHolder.Callback { 

    //SurfaceHolder sHolder; 
    //SurfaceView surfaceView; 
    Camera camera; 

    CamScreen(Context context) { 
    super(context);  
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     // Open the camera and start viewing  
     camera = Camera.open(); 

     try { 
      camera.setPreviewDisplay(holder); 
     } catch (IOException exception) { 
      camera.release(); 
      camera = null; 
     } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     // Kill all our crap with the surface 
     camera.stopPreview(); 
     camera.release(); 
     camera = null; 
    } 

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
     // Modify parameters to match size. 
     Camera.Parameters params = camera.getParameters(); 
     params.setPreviewSize(w, h); 
     params.setPictureFormat(PixelFormat.JPEG); 
     camera.setParameters(params); 

     camera.startPreview(); 
    } 
} 

任何想法有什麼不好?我認爲如果應用程序適用於較舊的Android版本,它也適用於新版本。

謝謝

+1

究竟哪個部分不起作用?沒有重疊?崩潰? – 2012-03-04 19:25:03

+2

[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – nfechner 2012-03-04 19:26:07

回答

1

的API 9級具有公共靜態相機打開(INT cameraId) 其中cameraid定義了前後攝像頭。請更改api級別的android jar並將公共Camera.open()更改爲Camera open(int cameraId)

+0

甚至使用更高的API和Camera.open(cameraId)不會阻止應用程序崩潰。而且,API lvl 9+中的camera.open()不應該默認打開後置攝像頭嗎? – user1248515 2012-03-04 20:12:54

+0

你有沒有調試過你的應用程序崩潰或在哪一點上............ – 2012-03-05 14:15:48

+0

在更高級別上更改API後,應用程序在仿真器上工作,但繼續下降到電話 – user1248515 2012-03-05 22:51:27

相關問題