Android相机开发实战,一篇文章帮你解答

本文深入探讨了Android相机开发,包括初始化相机、设置参数、图像方向处理和预览尺寸调整。通过示例代码展示了如何实现相机的正确显示和自动对焦功能,适合Android开发者参考。
摘要由CSDN通过智能技术生成

mCamera = camera;

initCamera();

}

public void initCamera() {

if (mCamera != null) {

Camera.Parameters params = mCamera.getParameters();

//mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();

requestLayout();

if (mPreviewSize == null) {

mPreviewSize = findBestPreviewResolution();

}

if (adapterSize == null) {

adapterSize = findBestPictureResolution();

}

if (adapterSize != null) {

params.setPictureSize(adapterSize.width, adapterSize.height);

}

if (mPreviewSize != null) {

params.setPreviewSize(mPreviewSize.width, mPreviewSize.height);

}

params.setPictureFormat(PixelFormat.JPEG);

List focusModes = params.getSupportedFocusModes();

if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {

// set the focus mode

params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

// set Camera parameters

mCamera.setParameters(params);

}

setDispaly(params, mCamera);

//setCameraDisplayOrientation((Activity) mContext, mCurrentCameraId, mCamera);

mCamera.setParameters(params);

}

}

//控制图像的正确显示方向

private void setDispaly(Camera.Parameters parameters, Camera camera) {

if (Build.VERSION.SDK_INT >= 8) {

setDisplayOrientation(camera, 90);

} else {

parameters.setRotation(90);

}

}

//实现的图像的正确显示

private void setDisplayOrientation(Camera camera, int i) {

Method downPolymorphic;

try {

downPolymorphic = camera.getClass().getMethod(“setDisplayOrientation”,

new Class[]{int.class});

if (downPolymorphic != null) {

downPolymorphic.invoke(camera, new Object[]{i});

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static void setCameraDisplayOrientation(Activity activity,

int cameraId, android.hardware.Camera camera) {

android.hardware.Camera.CameraInfo info =

new android.hardware.Camera.CameraInfo();

android.hardware.Camera.getCameraInfo(cameraId, info);

int rotation = activity.getWindowManager().getDefaultDisplay()

.getRotation();

int degrees = 0;

switch (rotation) {

case Surface.ROTATION_0:

degrees = 0;

break;

case Surface.ROTATION_90:

degrees = 90;

break;

case Surface.ROTATION_180:

degrees = 180;

break;

case Surface.ROTATION_270:

degrees = 270;

break;

}

int result;

if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {

result = (info.orientation + degrees) % 360;

result = (360 - result) % 360; // compensate the mirror

} else { // back-facing

result = (info.orientation - degrees + 360) % 360;

}

camera.setDisplayOrientation(result);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);

final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);

setMeasuredDimension(width, height);

// if (mSupportedPreviewSizes != null) {

// mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);

// }

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

if (changed && getChildCount() > 0) {

final View child = getChildAt(0);

final int width = r - l;

final int height = b - t;

int previewWidth = width;

int previewHeight = height;

if (mPreviewSize != null) {

previewWidth = mPreviewSize.width;

previewHeight = mPreviewSize.height;

}

// Center the child SurfaceView within the parent.

if (width * previewHeight > height * previewWidth) {

final int scaledChildWidth = previewWidth * height / previewHeight;

child.layout((width - scaledChildWidth) / 2, 0,

(width + scaledChildWidth) / 2, height);

} else {

final int scaledChildHeight = previewHeight * width / previewWidth;

child.layout(0, (height - scaledChildHeight) / 2,

width, (height + scaledChildHeight) / 2);

}

}

}

public void surfaceCreated(SurfaceHolder holder) {

// The Surface has been created, acquire the camera and tell it where

// to draw.

try {

if (mCamera != null) {

mCamera.setPreviewDisplay(holder);

}

} catch (IOException e) {

if (null != mCamera) {

mCamera.release();

mCamera = null;

}

e.printStackTrace();

}

}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

if (holder.getSurface() == null) {

return;

}

if (mCamera != null) {

Camera.Parameters parameters = mCamera.getParameters();

parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);

mCamera.setParameters(parameters);

try {

mCamera.setPreviewDisplay(holder);

} catch (IO

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值