Android regularly takes photos and sends Weibo messages

Source: Internet
Author: User
Tags date now

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. androi D. 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 andro Id. hardware. camera; import android. hardware. camera. autoFocusCallback; import android. hardware. camera. pictureCallback; import android. OS. bundle; import android. OS. environment; import android. OS. logoff; 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 "; // button panel private View mPannelController; // message header private Camera mCamera; public static oauth2accesen en accessToken; StatusesAPI api; @ Overridepublic void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); System. out. println ("onCreate"); requestWindowFeature (Window. FEATURE_NO_TITLE); getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. F LAG_FULLSCREEN); setContentView (R. layout. main); mPannelController = this. findViewById (R. id. buttonlayout); // do not use Timerpool = Executors to create a scheduled thread. newFixedThreadPool (1); // Preview Control SurfaceView surfaceView = (SurfaceView) this. findViewById (R. id. surfaceView); // set the surfaceView parameter. getHolder (). setFixedSize (176,144); surfaceView. getHolder (). setKeepScreenOn (true); surfaceView. getHolder (). setType (SurfaceHolder. SURFACE _ TYPE_PUSH_BUFFERS); surfaceView. getHolder (). addCallback (new SurfaceCallback (); // Add event final Button start = (Button) findViewById (R. id. takepicture); start. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (mCamera! = Null) {start. setEnabled (false); // before taking the photo, focus on the image to obtain the image mCamera. autoFocus (new AutoFocusCallback () {// focus end @ Overridepublic void onAutoFocus (boolean success, Camera camera) {// focus successful Toast. makeText (MainActivity. this, "Focus successful !! ", Toast. LENGTH_SHORT ). show (); mIsRunning = true; // 10 s one pool.exe cute (mRunnable); // mCamera. takePicture (null, null, new MyPictureCallback () ;}}}); Button end = (Button) findViewById (R. id. endtake); end. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {mIsRunning = false; // cancel the task 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 () {@ Overridepublic void onClick (View v) {mSsoHandler = new SsoHandler (MainActivity. this, mWeibo); mSsoHandler. authorize (new UserInfoListener () ;}});} class UserInfoListener implements WeiboAuthListener {@ Overridepublic void onCancel () {Log. v ("sina", "------ o NCancel ") ;}@ Overridepublic void onComplete (Bundle arg0) {String token = arg0.getString (" access_token "); String expires_in = arg0.getString (" expires_in "); accessToken = new Oauth2AccessToken (token, expires_in);} @ Overridepublic void onError (WeiboDialogError arg0) {Log. v ("sina", "------ onError" + arg0.getMessage () ;}@ Overridepublic void onWeiboException (WeiboException arg0) {Log. v ("sina", "------ onWeibo Exception "+ arg0.getMessage () ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (mSsoHandler! = Null) {Log. v ("time", "sso callback"); mSsoHandler. authorizeCallBack (requestCode, resultCode, data) ;}@overrideprotected void onDestroy () {super. onDestroy (); mIsRunning = false; if (pool! = Null) {pool. shutdownNow () ;}/// timer private ExecutorService pool = null; private boolean mIsRunning = true; // scheduled task private Runnable mRunnable = new Runnable () {@ Overridepublic void run () {// Method for triggering the camera at regular intervals while (mIsRunning) {mCamera. takePicture (null, null, new MyPictureCallback (); try {Thread. sleep (100*1000);} catch (InterruptedException e) {e. printStackTrace () ;}}}; public void share (String path) {if (acce SsToken. 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 + "test sharing", path, "0", "0", new RequestListener () {@ Overridepublic void onIOException (IOException arg0) {Log. v ("sina", "share failed" + arg0.getMessage () ;}@ Overridepublic vo Id onError (WeiboException arg0) {Log. v ("sina", "share failed" + arg0.getMessage () ;}@ Overridepublic void onComplete (String arg0) {Log. v ("sina", "shared successfully" + arg0) ;}} else {mSsoHandler = new SsoHandler (MainActivity. this, mWeibo); mSsoHandler. authorize (new UserInfoListener (); share (path) ;}// photo callback 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, "saved successfully !! "+ 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 () ;}}// the preview interface Callback private final class SurfaceCallback implements Callback {// The preview interface is created with public void surfaceCreated (SurfaceHolder holder) {try {System. out. Println ("surfaceCreated"); mCamera = Camera. open (); // open the Camera. parameters parameters = mCamera. getParameters (); parameters. setPreviewSize (800,480); parameters. setPreviewFrameRate (5); parameters. setPictureSize (1024,768); parameters. setshortquality (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 blocke. printStackTrace ();} mCamera. startPreview (); // start preview} // The preview interface is destroyed. public void surfaceDestroyed (SurfaceHolder holder) {System. out. println ("surfaceDestroyed"); if (mCamera! = Null) {mCamera. release (); mCamera = null ;}}@ Overridepublic boolean onTouchEvent (MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_DOWN) {mPannelController. setVisibility (ViewGroup. VISIBLE); return true;} return super. onTouchEvent (event );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.