Android的相机方向为新手

Android的相机方向为新手

本文介绍了Android的相机方向为新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用相机和摄像头方向面临的问题工作。我已经发现了一些互联网的答案,我想这个答案可能是最适合我的。

谁能给我详细的指令,<一个href=\"http://stackoverflow.com/questions/3841122/android-camera-$p$pview-is-sideways/5110406#5110406\">how在我的相机项目中添加这个答案?

下面是我的

直接code此处

 公共类AndroidCamera扩展活动实现SurfaceHolder.Callback { 摄像头摄像头;
 SurfaceView surfaceView;
 SurfaceHolder surfaceHolder;
 布尔previewing = FALSE;
 LayoutInflater controlInflater = NULL; 按钮buttonTakePicture; 最终诠释RESULT_SAVEIMAGE = 0;   / **当第一次创建活动调用。 * /
   @燮pressWarnings(德precation)
@覆盖
   公共无效的onCreate(捆绑savedInstanceState){
       super.onCreate(savedInstanceState);
       的setContentView(R.layout.main);
       。getWindow()和setFormat(PixelFormat.UNKNOWN);
       surfaceView =(SurfaceView)findViewById(R.id.camera preVIEW);
       surfaceHolder = surfaceView.getHolder();
       surfaceHolder.addCallback(本);
       surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
       controlInflater = LayoutInflater.from(getBaseContext());
       查看viewControl = controlInflater.inflate(R.layout.control,NULL);
       的LayoutParams layoutParamsControl
        =新的LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT);
       this.addContentView(viewControl,layoutParamsControl);
       buttonTakePicture =(按钮)findViewById(R.id.takepicture);
       buttonTakePicture.setOnClickListener(新Button.OnClickListener(){   @覆盖
   公共无效的onClick(查看为arg0){
    // TODO自动生成方法存根
    camera.takePicture(myShutterCallback,
      myPictureCallback_RAW,myPictureCallback_JPG);
   }});       的LinearLayout layoutBackground =(的LinearLayout)findViewById(R.id.background);
       layoutBackground.setOnClickListener(新LinearLayout.OnClickListener(){
   @覆盖
   公共无效的onClick(查看为arg0){
    // TODO自动生成方法存根    buttonTakePicture.setEnabled(假);
    camera.autoFocus(myAutoFocusCallback);
   }});
   }   AutoFocusCallback myAutoFocusCallback =新AutoFocusCallback(){  @覆盖
  公共无效onAutoFocus(布尔为arg0,ARG1相机){
   // TODO自动生成方法存根
   buttonTakePicture.setEnabled(真);
  }};   ShutterCallback myShutterCallback =新ShutterCallback(){  @覆盖
  公共无效onShutter(){
   // TODO自动生成方法存根  }}; PictureCallback myPictureCallback_RAW =新PictureCallback(){  @覆盖
  公共无效onPictureTaken(字节[]为arg0,ARG1相机){
   // TODO自动生成方法存根  }}; PictureCallback myPictureCallback_JPG =新PictureCallback(){  @覆盖
  公共无效onPictureTaken(字节[]为arg0,ARG1相机){
   // TODO自动生成方法存根
   / *位图bitmapPicture
    = BitmapFactory.de codeByteArray的(为arg0,0,arg0.length); * /   乌里uriTarget = getContentResolver()插入(Media.EXTERNAL_CONTENT_URI,新ContentValues​​());   的OutputStream imageFileOS;
   尝试{
    imageFileOS = getContentResolver()openOutputStream(uriTarget)。
    imageFileOS.write(为arg0);
    imageFileOS.flush();
    imageFileOS.close();    Toast.makeText(AndroidCamera.this,
      图像保存,
      Toast.LENGTH_SHORT).show();   }赶上(FileNotFoundException异常五){
    // TODO自动生成catch块
    e.printStackTrace();
   }赶上(IOException异常五){
    // TODO自动生成catch块
    e.printStackTrace();
   }   camera.start preVIEW();
  }}; @覆盖
 公共无效surfaceChanged(SurfaceHolder架,INT格式,宽度INT,
   INT高度){
  // TODO自动生成方法存根
  如果(previewing){
   camera.stop preVIEW();
   previewing = FALSE;
  }  如果(相机!= NULL){
   尝试{
    camera.set previewDisplay(surfaceHolder);
    camera.start preVIEW();
    previewing = TRUE;
   }赶上(IOException异常五){
    // TODO自动生成catch块
    e.printStackTrace();
   }
  }
 } @覆盖
 公共无效surfaceCreated(SurfaceHolder持有人){
  // TODO自动生成方法存根
  相机= Camera.open();
 } @覆盖
 公共无效surfaceDestroyed(SurfaceHolder持有人){
  // TODO自动生成方法存根
  camera.stop preVIEW();
  camera.release();
  摄像头= NULL;
  previewing = FALSE;
 }
}


解决方案

为了解决您的相机的方向改变了你的 surfaceCreated 函数从默认的Andr​​oid相机旋转摄像头景观模式默认为波泰特模式(因为你的活动是在纵向模式下):

  @覆盖
公共无效surfaceCreated(SurfaceHolder持有人){
   相机= Camera.open();
   参数参数= camera.getParameters();
   camera.setDisplayOrientation(90);
   camera.setParameters(参数);
}

您应用程序也崩溃,当您尝试并拍照......但就是手头没有问题......尽管它可能与你需要将下面的行添加到您的清单做的:

 &LT;使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/&GT;

:)

I am working with a camera and facing problem in camera orientation. I have found some answer over internet and I think this answer might be best fit for me.

Can anybody give me detail instruction, how to add this answer in my camera project.?

Here is my Full camera project Code.

Direct code here

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;
 }
}
解决方案

In order to fix your camera's orientation changed your surfaceCreated function to rotate the camera from the default android camera default of landscape mode to portait mode (because your activity is in portrait mode):

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

Your app also crashes when you try and take a picture... but that is not the issue at hand... though it might have to do with you needing to add the following line to your manifest:

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

:)

这篇关于Android的相机方向为新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 06:32