服务器之家

服务器之家 > 正文

Android实现拍照功能

时间:2022-02-27 16:22     来源/作者:tangcheng_ok

大家好,这是一个简单的拍照功能,很简单的界面,一个显示图像区域SurfaceView一个“拍照”按钮。直接上代码!

1、CameraDemoActivity.java(主界面)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package org.winplus.camera;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
 
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class CameraDemoActivity extends Activity{
 
 private final static String TAG = "CameraActivity";
 private SurfaceView surfaceView;
 private SurfaceHolder surfaceHolder;
 private Camera camera;
 private File picture;
 private Button btnSave;
  
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.main);
  setupViews();
 }
  
 private void setupViews(){
  surfaceView = (SurfaceView) findViewById(R.id.camera_preview); // Camera interface to instantiate components
  surfaceHolder = surfaceView.getHolder(); // Camera interface to instantiate components
  surfaceHolder.addCallback(surfaceCallback); // Add a callback for the SurfaceHolder
  surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
   
  btnSave = (Button) findViewById(R.id.save_pic);
   
  btnSave.setOnClickListener(new OnClickListener() {
    
   @Override
   public void onClick(View v) {
    takePic();
   }
  });
 }
  
  
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_CAMERA
    || keyCode == KeyEvent.KEYCODE_SEARCH) {
   takePic();
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }
 
 private void takePic() {
 
  camera.stopPreview();// stop the preview
 
  camera.takePicture(null, null, pictureCallback); // picture
 }
 
 // Photo call back
 Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
  //@Override
  public void onPictureTaken(byte[] data, Camera camera) {
   new SavePictureTask().execute(data);
   camera.startPreview();
  }
 };
 
 // save pic
 class SavePictureTask extends AsyncTask<byte[], String, String> {
  @Override
  protected String doInBackground(byte[]... params) {
   String fname = DateFormat.format("yyyyMMddhhmmss", new Date()).toString()+".jpg";
    
   Log.i(TAG, "fname="+fname+";dir="+Environment.getExternalStorageDirectory());
   //picture = new File(Environment.getExternalStorageDirectory(),fname);// create file
    
   picture = new File(Environment.getExternalStorageDirectory()+"/"+fname);
    
   try {
    FileOutputStream fos = new FileOutputStream(picture.getPath()); // Get file output stream
    fos.write(params[0]); // Written to the file
    fos.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
   return null;
  }
 }
 
 // SurfaceHodler Callback handle to open the camera, off camera and photo size changes
 SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
 
  public void surfaceCreated(SurfaceHolder holder) {
   Log.i(TAG, "surfaceCallback====");
   camera = Camera.open(); // Turn on the camera
   try {
    camera.setPreviewDisplay(holder); // Set Preview
   } catch (IOException e) {
    camera.release();// release camera
    camera = null;
   }
  }
 
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
   Log.i(TAG,"====surfaceChanged");
   Camera.Parameters parameters = camera.getParameters(); // Camera parameters to obtain
   parameters.setPictureFormat(PixelFormat.JPEG);// Setting Picture Format
//   parameters.set("rotation", 180); // Arbitrary rotation
   camera.setDisplayOrientation(0);
//   parameters.setPreviewSize(400, 300); // Set Photo Size
   camera.setParameters(parameters); // Setting camera parameters
   camera.startPreview(); // Start Preview
  }
 
  public void surfaceDestroyed(SurfaceHolder holder) {
   Log.i(TAG,"====surfaceDestroyed");
   camera.stopPreview();// stop preview
   camera.release(); // Release camera resources
   camera = null;
  }
 };
}

2、main.xml(布局文件)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
 
 <android.view.SurfaceView
  android:id="@+id/camera_preview"
  android:layout_width="800dip"
  android:layout_height="600dip"
  android:layout_alignParentTop="true"
  android:layout_centerInParent="true"
  android:layout_gravity="center_vertical|center_horizontal" />
 
 <Button
  android:id="@+id/save_pic"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/txt_save" />
 
</LinearLayout>

3、别忘了添加权限:

?
1
2
3
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />

源码下载:Android实现拍照功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/tangcheng_ok/article/details/7036871

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部