当前位置:  首页  >  PHP资讯  >  业界资讯

Android实现手机拍照功能

这篇文章主要介绍了Android实现手机拍照功能,感兴趣的小伙伴们可以参考一下

本文实例为大家讲解如何轻松实现Android手机拍照功能,分享给大家供大家参考。具体如下:

一、布局文件main.xml

 <&#63;xml version="1.0" encoding="utf-8"&#63;>  

二、MainActivity.java

 import java.io.File; import java.io.FileOutputStream; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.os.Environment; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity { private View layout; private Camera camera; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); layout = this.findViewById(R.id.buttonlayout); SurfaceView surfaceView = (SurfaceView) this .findViewById(R.id.surfaceView); surfaceView.getHolder() .setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceView.getHolder().setFixedSize(176, 144); surfaceView.getHolder().setKeepScreenOn(true); surfaceView.getHolder().addCallback(new SurfaceCallback()); } public void takepicture(View v) { if (camera != null) { switch (v.getId()) { case R.id.takepicture: camera.takePicture(null, null, new MyPictureCallback()); break; case R.id.autofocus: camera.autoFocus(null); break; } } } private final class MyPictureCallback implements PictureCallback { public void onPictureTaken(byte[] data, Camera camera) { try { File jpgFile = new File( Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); FileOutputStream outStream = new FileOutputStream(jpgFile); outStream.write(data); outStream.close(); camera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } } private final class SurfaceCallback implements Callback { public void surfaceCreated(SurfaceHolder holder) { try { camera = Camera.open();// 打开摄像头 Camera.Parameters parameters = camera.getParameters(); // Log.i("MainActivity", parameters.flatten()); parameters.setPreviewSize(800, 480); parameters.setPreviewFrameRate(5); parameters.setPictureSize(1024, 768); parameters.setJpegQuality(80); camera.setParameters(parameters); camera.setPreviewDisplay(holder); camera.startPreview();// 开始预览 } catch (Exception e) { e.printStackTrace(); } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } public void surfaceDestroyed(SurfaceHolder holder) { if (camera != null) { camera.release(); camera = null; } } } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { layout.setVisibility(ViewGroup.VISIBLE); return true; } return super.onTouchEvent(event); } } 

三、添加权限

      

效果如下:

希望本文所述对大家学习Android软件编程有所帮助。

内容推荐:免费高清PNG素材下载
吐了个 "CAO" !
扫码关注 PHP1 官方微信号
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有 京ICP备19059560号-4