Android自定义相机实现自动对焦和手动对焦

Lara ·
更新时间:2024-11-15
· 684 次阅读

Android自定义相机实现自动对焦和手动对焦:
不调用系统相机,因为不同的机器打开相机呈现的界面不统一也不能满足需求。
所以为了让程序在不同的机器上呈现出统一的界面,并且可以根据需求进行布局,做了此demo。

程序实现代码如下:

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Method; import android.graphics.PixelFormat; import android.hardware.Camera; import android.hardware.Camera.AutoFocusCallback; import android.hardware.Camera.Parameters; import android.hardware.Camera.PictureCallback; import android.hardware.Camera.ShutterCallback; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import com.hp.classes.config.Constants; import com.hp.classes.tools.CommonUtils; import com.hp.classes.ui.BaseActivity; @SuppressWarnings("deprecation") public class PhotographActivity extends BaseActivity implements OnClickListener, SurfaceHolder.Callback { private SurfaceView surfaceView; private Camera camera; private Camera.Parameters parameters; private Button btn_goback, btn_takephoto; private SurfaceHolder surfaceHolder; @Override protected void onDestroy() { super.onDestroy(); if(camera != null){ camera.stopPreview(); camera.release(); camera = null; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.classes_activity_takephoto); initView(); } @Override protected void onResume() { super.onResume(); initCamera(); } private void initView(){ btn_goback = (Button) findViewById(R.id.btn_goback); btn_goback.setOnClickListener(this); btn_takephoto = (Button) findViewById(R.id.btn_takephoto); btn_takephoto.setOnClickListener(this); surfaceView = (SurfaceView) findViewById(R.id.surfaceView); surfaceView.setFocusable(true); surfaceView.setOnClickListener(this); surfaceView.setBackgroundColor(TRIM_MEMORY_BACKGROUND); surfaceHolder = surfaceView.getHolder(); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceHolder.setKeepScreenOn(true); surfaceHolder.setFixedSize(400, 300); surfaceHolder.addCallback(this); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub camera.stopPreview(); camera.release(); camera = null; } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub try { camera.setPreviewDisplay(surfaceHolder); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // 实现自动对焦 camera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { camera.cancelAutoFocus();// 只有加上了这一句,才会自动对焦 doAutoFocus(); } } }); } // 相机参数的初始化设置 private void initCamera() { if (null == camera) { camera = Camera.open(); } parameters = camera.getParameters(); parameters.setPictureFormat(PixelFormat.JPEG); parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); if (!Build.MODEL.equals("KORIDY H30")) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1连续对焦 }else{ parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); } camera.setParameters(parameters); setDispaly(camera); camera.startPreview(); camera.cancelAutoFocus();// 2如果要实现连续的自动对焦,这一句必须加上 } // 控制图像的正确显示方向 private void setDispaly(Camera camera) { if (Integer.parseInt(Build.VERSION.SDK) >= 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) { Log.e("Came_e", "图像出错"); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.surfaceView: doAutoFocus(); break; case R.id.btn_takephoto: takePicture(); break; case R.id.btn_goback: finish(); break; default: break; } } // handle button auto focus private void doAutoFocus() { parameters = camera.getParameters(); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); camera.setParameters(parameters); camera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { camera.cancelAutoFocus();// 只有加上了这一句,才会自动对焦。 if (!Build.MODEL.equals("KORIDY H30")) { parameters = camera.getParameters(); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1连续对焦 camera.setParameters(parameters); }else{ parameters = camera.getParameters(); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); camera.setParameters(parameters); } } } }); } private void takePicture() { camera.takePicture(shutterCallback, rawCallback, jpegCallback); } // define shutterCallback ShutterCallback shutterCallback = new ShutterCallback() { public void onShutter() { // TODO Do something when the shutter closes. } }; PictureCallback rawCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { // TODO Do something with the image RAW data. } }; // stroe the picture in format jpeg PictureCallback jpegCallback = new PictureCallback() { public void onPictureTaken(byte[] data, Camera camera) { // Save the image JPEG data to the SD card FileOutputStream outStream = null; try { //修改图片路径和名称 String tempFilename = String.valueOf(System.currentTimeMillis()) + ".jpg"; File folder = Constants.CACHE_FOLDER; if (!folder.isDirectory()) { folder.mkdirs(); } String path = Constants.CACHE_FOLDER + File.separator + tempFilename; outStream = new FileOutputStream(path); outStream.write(data); outStream.flush(); outStream.close(); surfaceView.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeByteArray(data, 0, data.length))); } catch (FileNotFoundException e) { Log.e("TAG", "File Note Found", e); } catch (IOException e) { Log.e("TAG", "IO Exception", e); } } }; }

classes_activity_takephoto.xml代码如下:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <SurfaceView android:id="@+id/surfaceView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/rlright" /> <LinearLayout android:id="@+id/rlright" android:layout_width="40dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:background="#2b2b2b" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="0dip" android:layout_weight="1" android:gravity="center" > <Button android:id="@+id/btn_goback" style="@style/PETextButton" android:text="返回"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="0dip" android:layout_weight="1" android:gravity="center" > <Button android:id="@+id/btn_takephoto" style="@style/PETextButton" android:text="拍照" /> </LinearLayout> </LinearLayout> </RelativeLayout>

在清单文件需要添加相应权限:

<uses-permission android:name="android.permission.CAMERA"/> <uses-feature android:name="android.hardware.camera"/> <uses-feature android:name="android.hardware.camera.autofocus"/> 您可能感兴趣的文章:android 调用系统的照相机和图库实例详解Android 调用系统相机拍摄获取照片的两种方法实现实例Android启动相机拍照并返回图片Android调用相机并将照片存储到sd卡上实现方法Android 7.0调用相机崩溃详解及解决办法Android 简单的照相机程序的实例代码Android实现读取相机(相册)图片并进行剪裁Android自定义照相机详解android中打开相机、打开相册进行图片的获取示例Android仿最新微信相机功能



手动对焦 自动 自动对焦 相机 Android

需要 登录 后方可回复, 如果你还没有账号请 注册新账号