2013-01-23 76 views
3

我拿着相機攝像頭方向面臨的問題的工作。我通過互聯網找到了一些答案,我認爲這個答案可能最適合我。Android攝像頭方向爲新手

任何人都可以給我詳細的指令,how to add this answer在我的相機項目?

這裏是我的Full camera project Code.

直接代碼在這裏

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{ 

Camera camera; 
SurfaceView surfaceView; 
SurfaceHolder surfaceHolder; 
boolean previewing = false; 
LayoutInflater controlInflater = null; 

Button buttonTakePicture; 



final int RESULT_SAVEIMAGE = 0; 

    /** Called when the activity is first created. */ 
    @SuppressWarnings("deprecation") 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     getWindow().setFormat(PixelFormat.UNKNOWN); 
     surfaceView = (SurfaceView)findViewById(R.id.camerapreview); 
     surfaceHolder = surfaceView.getHolder(); 
     surfaceHolder.addCallback(this); 
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 


     controlInflater = LayoutInflater.from(getBaseContext()); 
     View viewControl = controlInflater.inflate(R.layout.control, null); 
     LayoutParams layoutParamsControl 
     = new LayoutParams(LayoutParams.FILL_PARENT, 
     LayoutParams.FILL_PARENT); 
     this.addContentView(viewControl, layoutParamsControl); 


     buttonTakePicture = (Button)findViewById(R.id.takepicture); 
     buttonTakePicture.setOnClickListener(new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    camera.takePicture(myShutterCallback, 
     myPictureCallback_RAW, myPictureCallback_JPG); 
    }}); 

     LinearLayout layoutBackground = (LinearLayout)findViewById(R.id.background); 
     layoutBackground.setOnClickListener(new LinearLayout.OnClickListener(){ 


    @Override 
    public void onClick(View arg0) { 
    // TODO Auto-generated method stub 

    buttonTakePicture.setEnabled(false); 
    camera.autoFocus(myAutoFocusCallback); 
    }}); 
    } 

    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){ 

    @Override 
    public void onAutoFocus(boolean arg0, Camera arg1) { 
    // TODO Auto-generated method stub 
    buttonTakePicture.setEnabled(true); 
    }}; 

    ShutterCallback myShutterCallback = new ShutterCallback(){ 

    @Override 
    public void onShutter() { 
    // TODO Auto-generated method stub 

    }}; 

PictureCallback myPictureCallback_RAW = new PictureCallback(){ 

    @Override 
    public void onPictureTaken(byte[] arg0, Camera arg1) { 
    // TODO Auto-generated method stub 

    }}; 

PictureCallback myPictureCallback_JPG = new PictureCallback(){ 

    @Override 
    public void onPictureTaken(byte[] arg0, Camera arg1) { 
    // TODO Auto-generated method stub 
    /*Bitmap bitmapPicture 
    = BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */ 

    Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues()); 

    OutputStream imageFileOS; 
    try { 
    imageFileOS = getContentResolver().openOutputStream(uriTarget); 
    imageFileOS.write(arg0); 
    imageFileOS.flush(); 
    imageFileOS.close(); 

    Toast.makeText(AndroidCamera.this, 
     "Image saved", 
     Toast.LENGTH_SHORT).show(); 

    } catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 

    camera.startPreview(); 
    }}; 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
    int height) { 
    // TODO Auto-generated method stub 
    if(previewing){ 
    camera.stopPreview(); 
    previewing = false; 
    } 

    if (camera != null){ 
    try { 
    camera.setPreviewDisplay(surfaceHolder); 
    camera.startPreview(); 
    previewing = true; 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 
} 



@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 
    camera = Camera.open(); 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 
    camera.stopPreview(); 
    camera.release(); 
    camera = null; 
    previewing = false; 
} 
} 
+1

@Vikalp Patel - 請停止提示僅在問題中插入粗體文本的編輯。這會分散注意力,並且不會提高這些問題的可讀性。 –

回答

0

爲了解決您的相機的方向改變了你的surfaceCreated功能,從風景模式默認的Android相機默認波泰特模式旋轉攝像頭(因爲您的活動處於肖像模式):

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    camera = Camera.open(); 
    Parameters parameters = camera.getParameters(); 
    camera.setDisplayOrientation(90); 
    camera.setParameters(parameters); 
} 

您的a PP也崩潰,當您嘗試並拍照......但就是手頭沒有問題......儘管它可能有你需要將下面的行添加到您的清單做:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

: )