前几天看到yeelink这个网站很感兴趣,注册了个图像传感器,就想着写个程序调用安卓摄像头并上传照片
程序很简单这个还是记录一下
首先是权限,分别是联网,相机,文件读取,照相和自动对焦的权限
新建一个类,实现如下接口
照相机必须继承自surfaceview
我是这样调用的。。。。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CameraView(this)); //设置View。。
}
所以这个程序打开直接就是摄像机画面,自动拍照然后上传 没有任何其他的功能
代码自动分段了貌似,以下代码全在cameraview这个类里面,MyThread是个内部类
public class CameraView extends SurfaceView implements SurfaceHolder.Callback, Camera.PictureCallback {
private SurfaceHolder holder;
private Camera camera;
private boolean af;
public CameraView(Context context) {//构造函数
super(context);
holder = getHolder();//生成Surface Holder
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//指定Push Buffer
}
public void surfaceCreated(SurfaceHolder holder) {//Surface生成事件的处理
try {
camera = Camera.open();//摄像头的初始化
Camera.Parameters p =camera.getParameters();
p.setPictureSize(1500, 2000); //设置照片分辨率,太大上传不了
camera.setParameters(p);
camera.setPreviewDisplay(holder);
//camera.setDisplayOrientation(90);
} catch (Exception e) {
}
}
private void data2file(byte[] w, String fileName) throws Exception {//将二进制数据转换为文件的函数
FileOutputStream out =null;
try {
out =new FileOutputStream(fileName);
out.write(w);
out.close();
} catch (Exception e) {
if (out !=null)
out.close();
throw e;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {//Surface改变事件的处理
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
camera.setParameters(parameters);//设置参数
camera.startPreview();//开始预览
new MyThread().start() ;//这个线程实现拍照保存上传
//camera.autoFocus(null);
// camera.takePicture(null, null, this);
}
class MyThread extends Thread implements PictureCallback{
@Override
public void run() {
super.run();
while(true){
System.out.println("拍照线程wu异常");
camera.autoFocus(null);
camera.takePicture(null, null, this);
try {
sleep(20000);//20秒执行一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
try {
String path = Environment.getExternalStorageDirectory() +"/test.jpg";
Log.v("path", path);
data2file(data, path);
//到这步为止完成了自动拍照并保存,下面这个线程是上传的 想要触摸拍照的话要改写下的onTouchEvent方法,我这不用
new Thread(){
@Override
public void run()
{
URL url = null;
try {
url = new URL(//我的yeelink的图像传感器接入网址
"http://api.yeelink.net/v1.0/device/10722/sensor/17619/photos");
} catch (MalformedURLException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = ((HttpURLConnection) url
.openConnection());
} catch (IOException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
try {
connection.setRequestMethod("POST");
} catch (ProtocolException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
connection.setRequestProperty("U-ApiKey", "6d34f50cfb70255ddcbacae9cd5b2583");//我的yeelinkd的验证key
try {
connection.connect();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
OutputStream out = null;
try {
out = connection.getOutputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int len;
byte[] buffer = new byte[1024];
// 读取文件
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(
"/storage/emulated/0/test.jpg");//上传图片的地址
//"/storage/sdcard1/Pictures/Saved Pictures/20130429_084309.jpg");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while((len = fileInputStream.read(buffer, 0, 1024)) != -1){
out.write(buffer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream input = null;
try {
input = connection.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while ((len = input.read(buffer)) != -1) {
Log.i("tag", "data:"
+ new String(buffer, 0, len));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.disconnect();
}
}.start();
} catch (Exception e) {
}
camera.startPreview();
}
}