안 드 로 이 드 는 정시 에 사진 을 찍 고 웨 이 보 기능 을 보 냅 니 다.
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);
}
}
화면 아래 그림:이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.