在android jellybean中自动对焦

时间:2013-02-12 12:54:35

标签: android camera autofocus

我正在开发一个小型的Android图像处理项目。

之前我在三星Galaxy Ace上测试我的代码并且代码很好。

现在我有一部新手机三星Galaxy grand,它拥有800万像素摄像头和Android软糖。

图像质量大大降低。看来相机不是自动曝光的。

我尝试了很多关于自动对焦的教程,但图像质量保持不变

屏幕截图:http://wikisend.com/download/527870/device-2013-02-12-181021.png

public class TrackLaser extends Activity {
      private Preview mPreview;
        private DrawOnTop mDrawOnTop;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);

        // Hide the window title.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Create our Preview view and set it as the content of our activity.
        // Create our DrawOnTop view.
        mDrawOnTop = new DrawOnTop(this);
        mPreview = new Preview(this, mDrawOnTop);
                setContentView(mPreview);
        addContentView(mDrawOnTop, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }

}
@SuppressLint("DrawAllocation")
class DrawOnTop extends View {
    Bitmap mBitmap;
    Paint mPaintBlack;
    Paint mPaintYellow;
    Paint mPaintRed;
    Paint mPaintWhite;
    Paint mPaintGreen;
    Paint mPaintBlue;
    byte[] mYUVData;
    int[] mRGBData;
    int x,y;
    int mImageWidth, mImageHeight;


    public DrawOnTop(Context context) {
        super(context);

                mBitmap = null;
        mYUVData = null;
        mRGBData = null;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //
        }  
    // end onDraw method

    static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {
        //    
    } 

// ----------------------------------------------------------------------

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;
    DrawOnTop mDrawOnTop;
    boolean mFinished;

    Preview(Context context, DrawOnTop drawOnTop) {
        super(context);

        mDrawOnTop = drawOnTop;
        mFinished = false;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);

    }

    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open();

        try {
           mCamera.setPreviewDisplay(holder);

           mCamera.autoFocus(myAutoFocusCallback);  
           // Preview callback used whenever new viewfinder frame is available
           mCamera.setPreviewCallback(new PreviewCallback() {
              public void onPreviewFrame(byte[] data, Camera camera)
              {
                  if ( (mDrawOnTop == null) || mFinished )
                      return;

                  if (mDrawOnTop.mBitmap == null)
                  {
                      // Initialize the draw-on-top companion
                        Camera.Parameters params = camera.getParameters();
                      mDrawOnTop.mImageWidth = params.getPreviewSize().width;
                      mDrawOnTop.mImageHeight = params.getPreviewSize().height;
                      mDrawOnTop.mBitmap = Bitmap.createBitmap(mDrawOnTop.mImageWidth, 
                      mDrawOnTop.mImageHeight, Bitmap.Config.RGB_565);
                      mDrawOnTop.mRGBData = new int[mDrawOnTop.mImageWidth * mDrawOnTop.mImageHeight]; 
                      mDrawOnTop.mYUVData = new byte[data.length];                    
                  }

                  // Pass YUV data to draw-on-top companion
                  System.arraycopy(data, 0, mDrawOnTop.mYUVData, 0, data.length);
                  mDrawOnTop.invalidate();
              }
           });
        } 
        catch (IOException exception) {
            mCamera.release();
            mCamera = null;
        }
    }       


    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mFinished = true;
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);


        mCamera.setParameters(parameters);
       mCamera.startPreview();
       mCamera.autoFocus(myAutoFocusCallback);

    }
    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){

          public void onAutoFocus(boolean arg0, Camera arg1) {
              Log.w("myApp", "AUTOFOCUSSED");
          }};

}

0 个答案:

没有答案