在Android中使用Camera API以不同的焦点模式拍照

时间:2014-10-21 10:49:03

标签: android camera

我需要创建一个应用程序,当我点击一张照片时,它将保存3个jpegs是不同的对焦模式(自动对焦,微距对焦和无限远对焦),如果可能的话并保存它。我已经制作了一个基本的相机应用程序,我发布的代码。你们可以告诉我如何实现这个吗?

public class MainActivity extends Activity implements  Constants,SurfaceHolder.Callback,Camera.PreviewCallback,
Camera.AutoFocusCallback, OnTouchListener{
private ViewFlipper myviewflipper;
private float initialXpoint;
private Camera mCamera;
private SurfaceView mPreviewSV;
private SurfaceHolder mSurfaceHolder;
private Button mCapture;
private Button mChange;
private Button mReturn;
private Button mLoad;
private ImageView mImageView;
private int mPicturesCaptured = 0;

boolean isCaptureClicked = false;
boolean encodingCompleted = false;

long startTime;
int currentFile = 0;
int initialPointer = -1;
int secondPointer = -1;
int direction = -1;

CameraSize picSize = BACK_CAM_PREVIEW_SIZE_3 ;
File mFile;
ArrayList<String> imagePath;
ProgressDialog mEncodingProgress;
DisplayTask mTask;
Bitmap mDisplayBitmap;
File path;
File[] files;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    mFile = new File(Constants.Cameraeffect);
    if (!mFile.exists())
        mFile.mkdir();
    setupUI();
}

private void setupUI()
{
    mPreviewSV = (SurfaceView) findViewById(R.id.sv_cam_preview);
    mCapture = (Button) findViewById(R.id.button_capture);
    mChange = (Button) findViewById(R.id.toggle);
    mReturn = (Button) findViewById(R.id.button_return);

    mImageView = (ImageView) findViewById(R.id.display);
    mImageView.setOnTouchListener(this);

    mLoad = (Button) findViewById(R.id.button_load);
    mSurfaceHolder = mPreviewSV.getHolder();
    mSurfaceHolder.addCallback(this);
    mCapture.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            deleteFiles();
            mPicturesCaptured = 0;
            isCaptureClicked = true;
            startTime=System.currentTimeMillis();
            progressUpdate();
        }
    });
    mChange.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            int res = getCurrentCameraId();
            switch(res){
                case 0:
                    stopCamera();
                    startCamera(Constants.FRONT_CAMERA);
                    break;
                case 1:
                    stopCamera();
                    startCamera(Constants.BACK_CAMERA);
                    break;  
            }
        }
    });

    mLoad.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            imagePath=ListAllFiles();
            mImageView.bringToFront();
            mImageView.setVisibility(View.VISIBLE);
            mImageView.setImageBitmap(decodeFile(imagePath.get(0)));
            mTask = new DisplayTask();
            mTask.execute("");
        }
    });
    mReturn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }
    });
}




private ArrayList<String> ListAllFiles() {
    ArrayList<String> tFileList = new ArrayList<String>();
    path= new File(Constants.Cameraeffect);
    if( path.exists() ) {
        files = path.listFiles();
        for(int i=0; i<files.length; i++) {
            tFileList.add(files[i].toString());
        }
    }
    return tFileList;
}

 public void deleteFiles() {
        path= new File(Constants.Cameraeffect);
        if( path.exists() ) {
            files = path.listFiles();
            for(int i=0; i<files.length; i++) {
                    files[i].delete();
            }
        }
    }
@SuppressWarnings("unused")
int getCurrentCameraId() {
    int mCameraId = -1;
    CameraInfo ci = new CameraInfo();
    for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) {
        Camera.getCameraInfo(i, ci);
        if (ci.facing == CameraInfo.CAMERA_FACING_BACK){
            return Constants.BACK_CAMERA;
        }
        else if (ci.facing == CameraInfo.CAMERA_FACING_FRONT){
            return Constants.FRONT_CAMERA;
        }
    }
    return -1; 
}

private void progressUpdate()
{
    mEncodingProgress = new ProgressDialog(this);
    mEncodingProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    mEncodingProgress.setMessage("Encoding");
    mEncodingProgress.setIndeterminate(false);
    mEncodingProgress.show();
    new Thread(new Runnable()
    {
        public void run()
        {
            while((System.currentTimeMillis()-startTime)<=5000)
            {
                try
                {
                    Thread.sleep(10);
                    mEncodingProgress.setProgress(mPicturesCaptured + 1);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

@SuppressWarnings("unused")
private void startCamera(int id)
{
    try
    {
        if (mCamera != null)
            stopCamera();
        mCamera = Camera.open(id);
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(1280,720);
        parameters.setFocusMode("infinite");
        parameters.setJpegQuality(100);
        parameters.setAutoExposureLock(true);
        parameters.setAutoWhiteBalanceLock(true);
        parameters.setRotation(0);
        mCamera.setDisplayOrientation(0);

            mCamera.setParameters(parameters);
        mCamera.setPreviewCallback(this);
        mCamera.setPreviewDisplay(mPreviewSV.getHolder());
        mCamera.startPreview();
    }
    catch (IOException e)
    {
        stopCamera();
    }
}

private void stopCamera()
{
    if (mCamera != null)
    {
        mCamera.stopPreview();
        mCamera.setPreviewCallback(null);
        mPreviewSV.getHolder().removeCallback(this);
        mCamera.release();
        mCamera = null;
    }
}

@Override
protected void onStart()
{
    super.onStart();
}

@Override
protected void onResume()
{
    super.onResume();
}

@Override
protected void onPause()
{
    stopCamera();
    super.onPause();
}
@Override
protected void onDestroy()
{
    stopCamera();
    super.onDestroy();
}
public void surfaceCreated(SurfaceHolder holder)
{

}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
    startCamera(0);
}

public void surfaceDestroyed(SurfaceHolder holder)
{

}

public void onPreviewFrame(byte[] data, Camera camera)
{
    if (System.currentTimeMillis()-startTime<=5000)
    {
        savingJpegToSDCard(data);
    }
    else 
    {
        if(mEncodingProgress!=null){
            mEncodingProgress.dismiss();
            mEncodingProgress=null;
            encodingCompleted=true;
        }
    }
    if(encodingCompleted){
        /*
         *   Log.i("","sent to JNI");
         */

        encodingCompleted=false;
    }
}

public void onAutoFocus(boolean arg0, Camera arg1) {
    // TODO Auto-generated method stub

}

public boolean onTouch(View arg0, MotionEvent event) {
    Log.d("","srikrishna:: ontouch");
    switch(event.getAction()){
    case(MotionEvent.ACTION_DOWN):
        initialPointer=(int) event.getX();
        break;
    case(MotionEvent.ACTION_MOVE):
        secondPointer=(int) event.getX();
        break;
    case(MotionEvent.ACTION_UP):
        initialPointer=(int) event.getX();
    }
    return true;
}

public Bitmap decodeFile(String fPath)
{
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;
    opts.inDither = false;
    opts.inPurgeable = true;
    opts.inInputShareable = true;
    BitmapFactory.decodeFile(fPath, opts);
    final int REQUIRED_SIZE = 500;
    int scale = 1;
    if (opts.outHeight > REQUIRED_SIZE || opts.outWidth > REQUIRED_SIZE)
    {
        final int heightRatio = Math
                .round((float) opts.outHeight / (float) REQUIRED_SIZE);
        final int widthRatio = Math
                .round((float) opts.outWidth / (float) REQUIRED_SIZE);
        scale = heightRatio < widthRatio ? heightRatio : widthRatio;//
    }

    opts.inJustDecodeBounds = false;
    opts.inSampleSize = scale;

    Bitmap bm = BitmapFactory.decodeFile(fPath, opts).copy(
            Bitmap.Config.RGB_565, false);
    return bm;
}

private void savingJpegToSDCard(final byte[] b1)
{
    try{ 
        Camera.Parameters parameters = mCamera.getParameters(); 
        Size size = parameters.getPreviewSize(); 
        Log.i("","abh size width and height "+size.width + " "+size.height);
        YuvImage image = new YuvImage(b1, parameters.getPreviewFormat(),size.width, size.height, null); 
        File file = new File(Constants.Cameraeffect + "/File" + mPicturesCaptured + ".jpeg");
        FileOutputStream filecon = new FileOutputStream(file); 
        image.compressToJpeg(new Rect(0, 0, image.getWidth(), image.getHeight()), 90,filecon); 
    } 
    catch (FileNotFoundException e) { 
        e.printStackTrace();
    } 
    mPicturesCaptured++;
}

class DisplayTask extends AsyncTask<String, Bitmap, Void>
{
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        currentFile = 0;
    }

    @Override
    protected Void doInBackground(String... params)
    {
        while(true){
            Log.i("","abh imagePath.size() "+imagePath.size());
            if(secondPointer-initialPointer >50)
            {
                Log.i("","abh currentFile "+currentFile);
                mDisplayBitmap = decodeFile(imagePath.get(currentFile));
                publishProgress(mDisplayBitmap);
                try
                {
                    Thread.sleep(10);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                if(currentFile<=90)
                    currentFile++;
                initialPointer=secondPointer;               
            }
        }
    }
    @Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
    }

    @Override
    protected void onProgressUpdate(Bitmap... values)
    {
        super.onProgressUpdate(values);
        mImageView.setImageBitmap(values[0]);
    }
}

}

1 个答案:

答案 0 :(得分:0)

在开始相机方法中,将对焦模式设置为所需的模式并启动相机。如果要在单击按钮时动态更改它,则必须使用所需属性调用start camera方法。确保你正在发布相机[这是非常imp]也注意到android中的自定义相机实现是非常错误[许多错误是开放只是护目镜并看到]我建议你仔细阅读并实现它。