我们有一个自定义的IP摄像头和一个同伴的Android服务,提供摄像头输入的字节[](通过socket通信)。
void onCameraData(byte[] picData)
我需要在方法调用之后做到以下几点,
由于相机饲料有20 FPS,我想最大的1000个图像存储到SD卡。这是检查从网络摄像机的图像数据被正确接收在应用程序侧。
void setImageViewWithByteData(byte[] picData) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
byte[] data = Arrays.copyOf(picData, picData.length);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if (ivImageView != null){
ivImageView.invalidate();
ivImageView.setImageBitmap(bitmap);
downloadFiles(bitmap);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
} synchronized void downloadFiles(Bitmap bmp){
new Thread(new Runnable() {
@Override
public void run() {
try{
if(counter <=1000) {//slip the files after 1000 frames
Bitmap bitmap = bmp;
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "ipcamera_" + (++counter) + ".jpg"); // the File to save , append increasing numeric counter to prevent files from getting overwritten.
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = bitmap; // obtaining the Bitmap
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // saving the Bitmap to a file compressed as a JPEG
fOut.flush();
fOut.close(); // do not forget to close the stream
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
bitmap.recycle();
bitmap = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}