Android相机开发实战,kotlin作者

本文详细介绍了在Android中进行相机开发的过程,包括设置图片和预览尺寸、自动对焦、图像方向调整等功能。通过调整Camera.Parameters,实现了最佳预览分辨率的选取,确保在不同设备上正确显示图像。此外,还涵盖了相机界面的布局和触摸焦点处理。
摘要由CSDN通过智能技术生成

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 (IOException e) {

e.printStackTrace();

}

mCamera.startPreview();

reAutoFocus();

}

}

public void surfaceDestroyed(SurfaceHolder holder) {

// Surface will be destroyed when we return, so stop the preview.

if (mCamera != null) {

mCamera.stopPreview();

}

}

/**

  • 最小预览界面的分辨率

*/

private static

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值