我正在开展一个项目,我需要Andorid手机拍照并在ESP8266
发出HTTP请求时通过FTP发送。
此时,我已经编写了整个应用程序,但它的关键部分无法正常工作,这是我发现从wifi模块收到“请求”的解决方法 - 我在我的项目中实现了Firebase API监听数据库表上的更改。
ESP正在向我的Apache服务器发送一个GET,该服务器运行一个改变数据库值的PHP文件,触发Android上的事件,该事件将调用该方法拍摄照片并发送它。
有一个问题:如果我通过按钮调用该方法拍摄照片,它可以正常工作,但如果我在Firebase事件处理程序中调用相同的方法,则只会使应用程序崩溃。
为了说清楚,我知道我的代码可能会让任何人比任何错误带来Android烦恼的近乎零的体验。但这只是一个解决财务问题的方法,可以通过购买IP摄像头解决我的需求。无论如何,我将不胜感激任何帮助。
代码:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_tirarFoto:
new Thread() {
@Override
public void run() {
takePicture();
}
}.start();
break;
}
}
@Override
public void onResume(){
super.onResume();
boolInicial = true; //This prevents the event to run on activity launch
ipRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!boolInicial) {
new Thread() {
@Override
public void run() {
takePicture();
}
}.start();
}else{
boolInicial = false;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void takePicture() {
cam = Camera.open();
if (cam != null) {
try {
cam.setPreviewTexture(surfaceTexture);
cam.stopPreview();
cam.startPreview();
cam.takePicture(null, null,new Camera.PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, Camera camera) {
foto = data;
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inPreferredConfig = Bitmap.Config.RGB_565;
bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(data), null, bfo);
iv_foto.setImageBitmap(bitmap);
thread.start();
camera.stopPreview();
camera.release();
camera = null;
ipRef.setValue("b");
boolInicial = true;
}
});
} catch (Exception ex) {
Log.d("TAG", "Can't take picture!" + ex.toString());
}
}
}
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
ByteArrayInputStream bs = new ByteArrayInputStream(foto);
try {
FTPClient mFtpClient = new FTPClient();
mFtpClient.setConnectTimeout(10 * 1000);
mFtpClient.connect(InetAddress.getByName("IP"));
mFtpClient.login("user", "passw");
mFtpClient.changeWorkingDirectory("ftp");
mFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
mFtpClient.storeFile("ftp.png", bs);
mFtpClient.logout();
mFtpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
});
我设法让这个工作,代码现在看起来像这样:
public void takePicture() {
cam = Camera.open();
if (cam != null) {
try {
cam.setPreviewTexture(surfaceTexture);
cam.stopPreview();
cam.startPreview();
cam.takePicture(null, null,new Camera.PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, Camera camera) {
foto = data;
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inPreferredConfig = Bitmap.Config.RGB_565;
bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(data), null, bfo);
iv_foto.setImageBitmap(bitmap);
new Thread() {
@Override
public void run() {
ByteArrayInputStream bs = new ByteArrayInputStream(foto);
try {
FTPClient mFtpClient = new FTPClient();
mFtpClient.setConnectTimeout(10 * 1000);
mFtpClient.connect(InetAddress.getByName("192.168.1.107"));
mFtpClient.login("vrech", "gabrieli");
mFtpClient.changeWorkingDirectory("ftp");
mFtpClient.setFileType(FTP.BINARY_FILE_TYPE);
mFtpClient.storeFile("ftp.png", bs);
mFtpClient.logout();
mFtpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
camera.stopPreview();
camera.release();
camera = null;
ipRef.setValue("b");
boolInicial = true;
}
});
} catch (Exception ex) {
Log.d("TAG", "Can't take picture!" + ex.toString());
}
}
}