本文主要是介绍安卓利用SurfaceView实现拍照功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安卓开发中,除了使用自带的相机外,还可以自己实现自定义相机,通常是使用SurfaceView来实现预览拍照功能。直接贴代码:主界面Activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SurfaceHolder.Callback {private SurfaceView camera_sf;private Button camera_btn;//安卓硬件相机private Camera mCamera;private SurfaceHolder mHolder;public static final String FILE_PATH = "filePath";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();}@Overrideprotected void onResume() {super.onResume();if (null == mCamera) {mCamera = getCustomCamera();if (mHolder != null) {//开户相机预览previceCamera(mCamera, mHolder);}}}@Overrideprotected void onStop() {super.onStop();if (null != mCamera) {mCamera.setPreviewCallback(null);//停止预览mCamera.stopPreview();//释放相机资源mCamera.release();mCamera = null;}}private void initViews() {this.camera_sf = (SurfaceView) findViewById(R.id.camera_sf);this.camera_btn = (Button) findViewById(R.id.camera_btn);this.camera_btn.setOnClickListener(this);camera_sf.setOnClickListener(this);//获取SurfaceView的SurfaceHolder对象mHolder = camera_sf.getHolder();//实现SurfaceHolder.Callback接口mHolder.addCallback(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.camera_sf://点击可以对焦if (null != mCamera)mCamera.autoFocus(null);break;case R.id.camera_btn://点击进行拍照startTakephoto();break;}}private void startTakephoto() {//获取到相机参数Camera.Parameters parameters = mCamera.getParameters();//设置图片保存格式parameters.setPictureFormat(ImageFormat.JPEG);//设置图片大小parameters.setPreviewSize(480, 720);//设置对焦parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);//设置自动对焦mCamera.autoFocus(new Camera.AutoFocusCallback() {@Overridepublic void onAutoFocus(boolean success, Camera camera) {if (success) {mCamera.takePicture(null, null, new Camera.PictureCallback() {@Overridepublic void onPictureTaken(byte[] data, Camera camera) {dealWithCameraData(data);}});}}});}//保存拍照数据private void dealWithCameraData(byte[] data) {FileOutputStream fos = null;String tempStr = "/sdcard/ldm/";//图片临时保存位置String fileName = tempStr + System.currentTimeMillis() + ".jpg";File tempFile = new File(fileName);try {fos = new FileOutputStream(fileName);//保存图片数据fos.write(data);fos.close();Intent intent = new Intent(MainActivity.this, ShowResultActivity.class);intent.putExtra(FILE_PATH, fileName);startActivity(intent);finish();} catch (Exception e) {e.printStackTrace();}}@Overridepublic void surfaceCreated(SurfaceHolder holder) {previceCamera(mCamera, holder);}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {mCamera.stopPreview();previceCamera(mCamera, holder);}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if (null != mCamera) {mCamera.setPreviewCallback(null);//停止预览mCamera.stopPreview();//释放相机资源mCamera.release();mCamera = null;}}private Camera getCustomCamera() {if (null == mCamera) {//使用Camera的Open函数开机摄像头硬件mCamera = Camera.open();//Camera.open()方法说明:2.3以后支持多摄像头,所以开启前可以通过getNumberOfCameras先获取摄像头数目,// 再通过 getCameraInfo得到需要开启的摄像头id,然后传入Open函数开启摄像头,// 假如摄像头开启成功则返回一个Camera对象}return mCamera;}private void previceCamera(Camera camera, SurfaceHolder holder) {try {//摄像头设置SurfaceHolder对象,把摄像头与SurfaceHolder进行绑定camera.setPreviewDisplay(holder);//调整系统相机拍照角度camera.setDisplayOrientation(90);//调用相机预览功能camera.startPreview();} catch (IOException e) {e.printStackTrace();}}
}
主界面布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><SurfaceView
android:id="@+id/camera_sf"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"/><Button
android:id="@+id/camera_btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="开始拍照"android:textSize="16sp"android:gravity="center"android:layout_margin="15dp"/>
</LinearLayout>
拍照后显示出来:
public class ShowResultActivity extends Activity {private ImageView result_iv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_result);result_iv = (ImageView) findViewById(R.id.result_iv);showPic2ImageView();}private void showPic2ImageView() {String filePath = getIntent().getStringExtra(MainActivity.FILE_PATH);if (!TextUtils.isEmpty(filePath)) {try {FileInputStream fis = new FileInputStream(filePath);Bitmap bitmap = BitmapFactory.decodeStream(fis);Matrix matrix = new Matrix();//通过Matrix把图片旋转90度matrix.setRotate(90);bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);result_iv.setImageBitmap(bitmap);} catch (FileNotFoundException e) {e.printStackTrace();}}}
}
这个界面的布局就只一个ImageView用来显示图片,代码就省了。不要忘记添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><uses-permission android:name="android.permission.CAMERA"/>
这篇关于安卓利用SurfaceView实现拍照功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!