智能手机中的摄像头和普通手机中的摄像头最大的区别在于,智能机上的摄像头可以由程序员写程序控制,做一些有趣的应用譬如,画中画,做一些有用的应用譬如二维码识别,等等。本讲打算通过一个实例,来介绍一下摄像头编程,相关解释都写在代码中了,请注意看代码注释。
实例:窈窈照相机,功能很简单,就是点击程序弹出照相预览界面,点击相机按钮完成照相功能,所照相片会存储在手机存储卡根目录。
1、创建一个项目 Lesson36_Camera ,主程序文件为 MainActivity.java。
2、AndroidManifest.xml 中设置屏幕为横屏,并且声明摄像头和存储卡的使用权限,具体代码如下:
XML/HTML代码
- <?xml version="1.0" encoding="utf-8"?>
- <MANIFEST android:versionname="1.0" android:versioncode="1" xmlns:android="http://schemas.android.com/apk/res/android" package="basic.android.lesson36">
- <APPLICATION android:icon="@drawable/icon" android:label="@string/app_name">
- <ACTIVITY android:name=".MainActivity" android:label="@string/app_name" android:configchanges="orientation|keyboardHidden|keyboard" android:screenorientation="landscape">
- <INTENT -filter>
- <ACTION android:name="android.intent.action.MAIN" />
- <CATEGORY android:name="android.intent.category.LAUNCHER" />
- </INTENT>
- </ACTIVITY>
-
- </APPLICATION>
- <USES android:minsdkversion="8" -sdk />
- <USES android:name="android.permission.CAMERA" -permission></USES>
- <USES android:name="android.permission.WRITE_EXTERNAL_STORAGE" -permission></USES>
- <USES android:name="android.hardware.camera" -feature />
- <USES android:name="android.hardware.camera.autofocus" -feature />
- </MANIFEST>
3、本例中不需要布局文件main.xml,因为本例中的UI组建都是动态添加上去的。
4、最后MainActivity.java的代码如下:
Java代码
- package basic.android.lesson36;
-
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Calendar;
- import java.util.Locale;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- import android.os.Bundle;
- import android.text.format.DateFormat;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.FrameLayout;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
-
- private CameraView cv;
-
- private Camera mCamera = null;
-
- private Bitmap mBitmap = null;
-
-
- public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
-
- public void onPictureTaken(byte[] data, Camera camera) {
- Log.i("yao","onPictureTaken");
- Toast.makeText(getApplicationContext(), "正在保存……", Toast.LENGTH_LONG).show();
-
- mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
-
- File file = new File("/sdcard/YY"+ new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg");
- try {
- file.createNewFile();
- BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
- mBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
- os.flush();
- os.close();
- Toast.makeText(getApplicationContext(), "图片保存完毕,在存储卡的根目录", Toast.LENGTH_LONG).show();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- };
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- getWindow().setFormat(PixelFormat.TRANSLUCENT);
-
-
- FrameLayout fl = new FrameLayout(this);
-
-
- cv = new CameraView(this);
- fl.addView(cv);
-
-
- TextView tv = new TextView(this);
- tv.setText("请按\"相机\"按钮拍摄");
- fl.addView(tv);
-
-
- setContentView(fl);
-
- }
-
-
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- Log.i("yao","MainActivity.onKeyDown");
- if (keyCode == KeyEvent.KEYCODE_CAMERA) {
- if (mCamera != null) {
- Log.i("yao","mCamera.takePicture");
-
- mCamera.takePicture(null, null, pictureCallback);
- }
- }
- return cv.onKeyDown(keyCode, event);
- }
-
-
- class CameraView extends SurfaceView {
-
- private SurfaceHolder holder = null;
-
-
- public CameraView(Context context) {
- super(context);
- Log.i("yao","CameraView");
-
-
- holder = this.getHolder();
-
- holder.addCallback(new SurfaceHolder.Callback() {
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
-
- mCamera.stopPreview();
-
- mCamera.release();
- mCamera = null;
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
-
- mCamera = Camera.open();
- try {
-
- mCamera.setPreviewDisplay(holder);
- } catch (IOException e) {
-
- mCamera.release();
- mCamera = null;
- }
-
- }
-
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
-
-
- Camera.Parameters parameters = mCamera.getParameters();
-
- parameters.setPictureFormat(PixelFormat.JPEG);
-
- parameters.setPreviewSize(854, 480);
-
- parameters.setFocusMode("auto");
-
- parameters.setPictureSize(2592, 1456);
-
- mCamera.setParameters(parameters);
-
- mCamera.startPreview();
- }
- });
-
- holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- }
-
- }
-
- }
5、连接Milestone,编译并运行程序:悲剧的是,截图只能看到黑黑的一片,无法截取到摄像头传输过来SurfaceView信息,而在真机中是能看到预览效果的。
还是上一张照好的图片给大家吧,(用了好多年的小黑……)
好了,本讲就到这里,谢谢大家的支持和鼓励,下次再见。