使用后置摄像头拍摄的图像模糊

时间:2017-03-01 22:40:12

标签: android

我正在从我的Android应用程序使用后置摄像头捕获图像,我使用的代码工作正常,但问题是捕获的图像模糊,我使用setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);但仍然是图片被捕获模糊。那么请告诉我如何摆脱这种模糊效果?

代码:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.os.Environment;
import android.view.SurfaceView;
import java.io.File;
import java.io.FileOutputStream;


public class PhotoCapture {

    public static void takeSnapShots(Context context) {

        SurfaceView surface = new SurfaceView(context);
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera camera = Camera.open();
        int cameraCount = Camera.getNumberOfCameras();

        Camera.Parameters p = camera.getParameters();
        p.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        camera.setParameters(p);

        try {
            camera.setPreviewDisplay(surface.getHolder());
            camera.startPreview();
            camera.takePicture(null, null, jpegCallback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {
            String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator+"picure.jpg";
            File pictureFile = new File(path);

            try {
                FileOutputStream outputStream = new FileOutputStream(pictureFile);
                Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
                Matrix mtx = new Matrix();
                mtx.setRotate(90);
                realImage = Bitmap.createBitmap(realImage, 0, 0, realImage.getWidth(), realImage.getHeight(), mtx, true);
                realImage.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                outputStream.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                camera.stopPreview();
                camera.setPreviewCallback(null);
                camera.release();
                camera = null;
            }
        }
    };
}

1 个答案:

答案 0 :(得分:1)

这个怎么样?

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