最近在做android方面的开发,下面是android自动对焦并拍照的小例子:
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
package com.comnvi.camera; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.comnvi.camera.share.AccessTokenKeeper; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.weibo.oauthv2.OAuthV2; import com.weibo.sdk.android.Oauth2AccessToken; import com.weibo.sdk.android.Weibo; import com.weibo.sdk.android.WeiboAuthListener; import com.weibo.sdk.android.WeiboDialogError; import com.weibo.sdk.android.WeiboException; import com.weibo.sdk.android.api.StatusesAPI; import com.weibo.sdk.android.net.RequestListener; import com.weibo.sdk.android.sso.SsoHandler; import android.app.Activity; import android.content.Intent; import android.hardware.Camera; import android.hardware.Camera.AutoFocusCallback; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.os.Environment; import android.os.Looper; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity{ private Weibo mWeibo; SsoHandler mSsoHandler; public static final String SINA_CONSUMER_KEY = "2022960751" ; public static final String SINA_REDIRECT_URL = "http://sns.whalecloud.com/sina2/callback" ; // 按钮面板 private View mPannelController; // 报像头 private Camera mCamera; public static Oauth2AccessToken accessToken; StatusesAPI api; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); System.out.println( "onCreate" ); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); mPannelController = this .findViewById(R.id.buttonlayout); // 创建 定时线程 注意不要使用Timer pool = Executors.newFixedThreadPool( 1 ); // 预览控件 SurfaceView surfaceView = (SurfaceView) this .findViewById(R.id.surfaceView); // 设置参数 surfaceView.getHolder().setFixedSize( 176 , 144 ); surfaceView.getHolder().setKeepScreenOn( true ); surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceView.getHolder().addCallback( new SurfaceCallback()); // 添加事件 final Button start = (Button) findViewById(R.id.takepicture); start.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { if (mCamera != null ) { start.setEnabled( false ); // 拍照前需要对焦 获取清析的图片 mCamera.autoFocus( new AutoFocusCallback() { // 对焦结束 @Override public void onAutoFocus( boolean success, Camera camera) { // 对焦成功 Toast.makeText(MainActivity. this , "对焦成功 !!" , Toast.LENGTH_SHORT).show(); mIsRunning = true ; // 10s 一张 pool.execute(mRunnable); //mCamera.takePicture(null, null, new MyPictureCallback()); } }); } } }); Button end = (Button) findViewById(R.id.endtake); end.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { mIsRunning = false ; // 取消任务 start.setEnabled( true ); } }); mWeibo = Weibo.getInstance(SINA_CONSUMER_KEY, SINA_REDIRECT_URL); accessToken = AccessTokenKeeper.readAccessToken( this ); Button sina = (Button) findViewById(R.id.sina); sina.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { mSsoHandler = new SsoHandler(MainActivity. this , mWeibo); mSsoHandler.authorize( new UserInfoListener()); } }); } class UserInfoListener implements WeiboAuthListener{ @Override public void onCancel() { Log.v( "sina" , "------onCancel" ); } @Override public void onComplete(Bundle arg0) { String token = arg0.getString( "access_token" ); String expires_in = arg0.getString( "expires_in" ); accessToken = new Oauth2AccessToken( token, expires_in); } @Override public void onError(WeiboDialogError arg0) { Log.v( "sina" , "------onError" +arg0.getMessage()); } @Override public void onWeiboException(WeiboException arg0) { Log.v( "sina" , "------onWeiboException" +arg0.getMessage()); } } @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (mSsoHandler != null ) { Log.v( "time" , "sso回调" ); mSsoHandler.authorizeCallBack(requestCode, resultCode, data); } } @Override protected void onDestroy() { super .onDestroy(); mIsRunning = false ; if (pool != null ) { pool.shutdownNow(); } } // 定时器 private ExecutorService pool = null ; private boolean mIsRunning = true ; // 定时任务 private Runnable mRunnable = new Runnable() { @Override public void run() { // 定时触发拍照方法 while (mIsRunning) { mCamera.takePicture( null , null , new MyPictureCallback()); try { Thread.sleep( 100 * 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } } } }; public void share(String path){ if (accessToken.isSessionValid()&&! "" .equals(path)) { AccessTokenKeeper.keepAccessToken(MainActivity. this , accessToken); api = new StatusesAPI(accessToken); Date now= new Date(); SimpleDateFormat temp= new SimpleDateFormat( "MM月dd日 HH:mm" ); String str=temp.format(now); api.upload(str+ "测试分享" , path, "0" , "0" , new RequestListener() { @Override public void onIOException(IOException arg0) { Log.v( "sina" , "分享失败" +arg0.getMessage()); } @Override public void onError(WeiboException arg0) { Log.v( "sina" , "分享失败" +arg0.getMessage()); } @Override public void onComplete(String arg0) { Log.v( "sina" , "分享成功" +arg0); } }); } else { mSsoHandler = new SsoHandler(MainActivity. this , mWeibo); mSsoHandler.authorize( new UserInfoListener()); share(path); } } // 照片回调 private final class MyPictureCallback implements PictureCallback { // 照片生成后 public void onPictureTaken( byte [] data, Camera camera) { try { System.out.println( "onPictureTaken" ); File jpgFile = new File( Environment.getExternalStorageDirectory()+ "/ceshi" ); if (!jpgFile.exists()) { jpgFile.mkdir(); } File jpgFile1 = new File( jpgFile.getAbsoluteFile(), System.currentTimeMillis() + ".jpg" ); System.out.println(jpgFile1.getAbsolutePath()); Toast.makeText(MainActivity. this , "保存成功 !!" + jpgFile1.getAbsolutePath(), Toast.LENGTH_SHORT).show(); FileOutputStream outStream = new FileOutputStream(jpgFile1); outStream.write(data); outStream.close(); share(jpgFile1.getAbsolutePath()); camera.startPreview(); } catch (Exception e) { e.printStackTrace(); } } } // 预览界面回调 private final class SurfaceCallback implements Callback { // 预览界面被创建 public void surfaceCreated(SurfaceHolder holder) { try { System.out.println( "surfaceCreated" ); mCamera = Camera.open(); // 打开摄像头 Camera.Parameters parameters = mCamera.getParameters(); parameters.setPreviewSize( 800 , 480 ); parameters.setPreviewFrameRate( 5 ); parameters.setPictureSize( 1024 , 768 ); parameters.setJpegQuality( 80 ); mCamera.setParameters(parameters); } catch (Exception e) { e.printStackTrace(); } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { System.out.println( "surfaceChanged" ); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mCamera.startPreview(); // 开始预览 } // 预览界面被销毁 public void surfaceDestroyed(SurfaceHolder holder) { System.out.println( "surfaceDestroyed" ); if (mCamera != null ) { mCamera.release(); mCamera = null ; } } } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { mPannelController.setVisibility(ViewGroup.VISIBLE); return true ; } return super .onTouchEvent(event); } } |
界面如下图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xiaona1047985204/article/details/9369465