您好,登錄后才能下訂單哦!
小編給大家分享一下android如何實現定時拍照并發送微博功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
最近在做android方面的開發,下面是android自動對焦并拍照的小例子:
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.Oauth3AccessToken; 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 Oauth3AccessToken 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 Oauth3AccessToken( 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); } }
界面如下圖:
看完了這篇文章,相信你對“android如何實現定時拍照并發送微博功能”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。