在自定义相机中没有得到清晰的Android图像捕获

时间:2016-09-14 12:13:02

标签: android camera

创建了使用自定义相机拍摄照片的应用程序,但相机未像Intent相机那样以清晰可见的方式捕捉图像。这是代码:

Custom_CameraActivity.java: -

public class Custom_CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mCameraPreview;
Context context;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    context = getApplicationContext();
    mCamera = getCameraInstance();
    mCameraPreview = new CameraPreview(this, mCamera);
    SurfaceView preview = (SurfaceView) findViewById(R.id.camera_preview);
    preview.addView(mCameraPreview);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCamera.takePicture(null, null, mPicture);
        }
    });
}

/**
 * Helper method to access the camera returns null if it cannot get the
 * camera or does not exist
 *
 * @return
 */
private Camera getCameraInstance() {
    Camera camera = null;
    try {
        camera = Camera.open();
        Camera.Parameters parameters = camera.getParameters();
        parameters.set("jpeg-quality", 100);
        parameters.setPictureFormat(PixelFormat.JPEG);
        parameters.setPictureSize(800, 600);
        parameters.setRotation(180);
        camera.setDisplayOrientation(90);
        camera.setParameters(parameters);
    } catch (Exception e) {
        // cannot get camera or does not exist
    }
    return camera;
}

Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        try {
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            if (bitmap.getWidth() > bitmap.getHeight()) {
                Matrix matrix = new Matrix();
                matrix.postRotate(180);
                bitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            }
            } catch (Exception e) {

        }
    }
};

}

CameraPreview.java: -

public class CameraPreview extends SurfaceView implements
    SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;

// Constructor that obtains context and camera
@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
    super(context);
    this.mCamera = camera;
    this.mSurfaceHolder = this.getHolder();
    this.mSurfaceHolder.addCallback(this);
    this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try {
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        // left blank for now
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    mCamera.stopPreview();
    mCamera.release();
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
                           int width, int height) {
    // start preview with new settings
    try {
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (Exception e) {
        // intentionally left blank for a test
    }
}}

enter image description here

在此图片中,无法读取字母。

2 个答案:

答案 0 :(得分:2)

通过添加以下代码解决了问题: -

Camera.Parameters params = c.getParameters();
if (params.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
} else {
    params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
c.setParameters(params);

答案 1 :(得分:1)

试试这个

private Camera getCameraInstance() {
Camera camera = null;
try {
    camera = Camera.open();
    Camera.Parameters parameters = camera.getParameters();
    parameters.set("s3d-prv-frame-layout", "none");
    parameters.set("s3d-cap-frame-layout", "none");
    parameters.set("iso", "auto");
    parameters.set("contrast", 100);
    parameters.set("brightness", 50);
    parameters.set("saturation", 100);
    parameters.set("sharpness", 100);
    parameters.setAntibanding("auto");
    parameters.setPictureFormat(ImageFormat.JPEG);
    parameters.set("jpeg-quality", 100);
    if (params.isZoomSupported())
        params.setZoom(0);       
    parameters.setPictureSize(800, 600);
    parameters.setRotation(180);
    camera.setDisplayOrientation(90);
    camera.setParameters(parameters);
} catch (Exception e) {
    // cannot get camera or does not exist
}
    return camera;
}

编辑1。

Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);