本文介绍了在那里把自动对焦的类Android摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用autofoucs我不知道往哪里放自动对焦任何人有一个想法如何实现自动对焦?

---------- preVIEW类----------

 包com.marakana;

 进口java.io.IOException异常;

 进口android.content.Context;
 进口android.hardware.Camera;
 进口android.hardware.Camera previewCallback。
 进口android.util.Log;
 进口android.view.SurfaceHolder;
 进口android.view.SurfaceView;

类preVIEW延伸SurfaceView实现SurfaceHolder.Callback {//&其中1为卤素;
私有静态最后字符串变量=preVIEW;

SurfaceHolder mHolder; // 2>
公共摄像头摄像头; //&其中3的密度;

preVIEW(上下文的背景下){
超(上下文);

//安装SurfaceHolder.Callback所以我们得到通知时,该
//下垫面创建和销毁。
mHolder = getHolder(); //&所述; 4为H.
mHolder.addCallback(本); //&其中5个;
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //&其中; 6个
}

 //调用一次支架已准备就绪
公共无效surfaceCreated(SurfaceHolder持有者){//&其中7个;
//表面经创建,获取摄像机,并告诉它在哪里
//绘制。
相机= Camera.open(); //&其中8是氢;
尝试 {
  camera.set previewDisplay(保持器); //&其中9个;

  camera.set previewCallback(新previewCallback(){//小于10>
    //呼吁每一帧previewed
    在previewFrame(byte []的数据,摄像头摄像头){//&LT公共无效; 11>
      Log.d(关于previewFrame所谓的标签+ System.currentTimeMillis的());
      preview.this.invalidate(); //&其中12所取代;
     }
   });
 }赶上(IOException异常E){//< 13>
  e.printStackTrace();
   }
  }

 //当支架被破坏时调用
公共无效surfaceDestroyed(SurfaceHolder持有者){//&所述14取代;
camera.stop preVIEW();
摄像头= NULL;
}

//调用时,持有人已变更
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,INT W,INT高){//< 15>
  camera.start preVIEW();
 }

}
 

-----------------摄像头活动------------------------

  preVIEW =新的preVIEW(本); //&其中3的密度;
((的FrameLayout)findViewById(R.id. preVIEW))addView(preVIEW)。 //&所述; 4为H.

buttonClick =(按钮)findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(新OnClickListener(){
  公共无效的onClick(视图v){//小于5>

      preview.camera.takePicture(shutterCallback,rawCallback,jpegCallback);
  }
});

//处理数据的JPEG图片
PictureCallback jpegCallback =新PictureCallback(){//&所述8是氢;
公共无效onPictureTaken(byte []的数据,摄像头摄像头){
  FileOutputStream中outStream = NULL;
  尝试 {
    //写入SD卡

    outStream =新的FileOutputStream(imgPath); //&其中9个;
    outStream.write(数据);
    outStream.close();
    intImg =新的意图(CameraActivity.this,DisplayImg.class);
    intImg.putExtra(imgPath,imgPath);
    startActivity(intImg);
    Log.d(TAG,onPictureTaken  - 写字节:+ data.length);
  }赶上(FileNotFoundException异常E){//小于10>
    e.printStackTrace();
  }赶上(IOException异常E){
    e.printStackTrace();
  } 最后 {
  }
  Log.d(TAG,onPictureTaken  -  JPEG);
}
};
 

解决方案

如果您有自动对焦设置,你应该做的是这样的:

  camera.autoFocus(新AutoFocusCallback(){
        @覆盖
        公共无效onAutoFocus(布尔成功,相机摄像头){
            如果(成功){
                camera.takePicture(shutterCallback,rawCallback,jpegCallback);
            }
        }
    });
 

就是这样的相机等待自动对焦;)

I am trying to use autofoucs i have no idea where to put autofocus anyone has an idea how to implement autofocus?

----------preview class----------

 package com.marakana;

 import java.io.IOException;

 import android.content.Context;
 import android.hardware.Camera;
 import android.hardware.Camera.PreviewCallback;
 import android.util.Log;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;

class Preview extends SurfaceView implements SurfaceHolder.Callback { // <1>
private static final String TAG = "Preview";

SurfaceHolder mHolder;  // <2>
public Camera camera; // <3>

Preview(Context context) {
super(context);

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();  // <4>
mHolder.addCallback(this);  // <5>
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>
}

 // Called once the holder is ready
public void surfaceCreated(SurfaceHolder holder) {  // <7>
// The Surface has been created, acquire the camera and tell it where
// to draw.
camera = Camera.open(); // <8>
try {
  camera.setPreviewDisplay(holder);  // <9>

  camera.setPreviewCallback(new PreviewCallback() { // <10>
    // Called for each frame previewed
    public void onPreviewFrame(byte[] data, Camera camera) {  // <11>
      Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
      Preview.this.invalidate();  // <12>
     }
   });
 } catch (IOException e) { // <13>
  e.printStackTrace();
   }
  }

 // Called when the holder is destroyed
public void surfaceDestroyed(SurfaceHolder holder) {  // <14>
camera.stopPreview();
camera = null;
}

// Called when holder has changed
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15>
  camera.startPreview();
 }

}

-----------------camera activity------------------------

preview = new Preview(this); // <3>
((FrameLayout) findViewById(R.id.preview)).addView(preview); // <4>

buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
  public void onClick(View v) { // <5>

      preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
  }
});

// Handles data for jpeg picture
PictureCallback jpegCallback = new PictureCallback() { // <8>
public void onPictureTaken(byte[] data, Camera camera) {
  FileOutputStream outStream = null;
  try {
    // Write to SD Card

    outStream = new FileOutputStream(imgPath); // <9>
    outStream.write(data);
    outStream.close();
    intImg = new Intent(CameraActivity.this, DisplayImg.class);
    intImg.putExtra("imgPath",imgPath);
    startActivity(intImg);
    Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
  } catch (FileNotFoundException e) { // <10>
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
  }
  Log.d(TAG, "onPictureTaken - jpeg");
}
};
解决方案

If you have autofocus set, you should do something like this:

camera.autoFocus(new AutoFocusCallback() {
        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            if(success){
                camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            }
        }
    });

That's the way the camera waits for the autofocus ;)

这篇关于在那里把自动对焦的类Android摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 04:33