/* Save the buffered data into a JPG image file*/
private void screenShot(byte[] buf, String shotDir) {
File dir = new File(shotDir);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdirs();
}
YuvImage yuvImage = new YuvImage(buf,
ImageFormat.NV21,
DJIVideoStreamDecoder.getInstance().width,
DJIVideoStreamDecoder.getInstance().height,
null);
OutputStream outputFile;
final String path = dir + "/ScreenShot_" + System.currentTimeMillis() + ".jpg";
try {
outputFile = new FileOutputStream(new File(path));
} catch (FileNotFoundException e) {
Log.e(TAG, "test screenShot: new bitmap output file error: " + e);
return;
}
if (outputFile != null) {
yuvImage.compressToJpeg(new Rect(0,
0,
DJIVideoStreamDecoder.getInstance().width,
DJIVideoStreamDecoder.getInstance().height), 100, outputFile);
}
try {
outputFile.close();
} catch (IOException e) {
Log.e(TAG, "test screenShot: compress yuv image error: " + e);
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
displayPath(path);
}
});
}
public void onClick(View v) {
if (screenShot.isSelected()) {
screenShot.setText("Screen Shot");
screenShot.setSelected(false);
if (useSurface) {
DJIVideoStreamDecoder.getInstance().changeSurface(videostreamPreviewSh.getSurface());
}
savePath.setText("");
savePath.setVisibility(View.INVISIBLE);
} else {
screenShot.setText("Live Stream");
screenShot.setSelected(true);
if (useSurface) {
DJIVideoStreamDecoder.getInstance().changeSurface(null);
}
savePath.setText("");
savePath.setVisibility(View.VISIBLE);
pathList.clear();
}
}
private void displayPath(String path){
path = path + "\n\n";
if(pathList.size() < 6){
pathList.add(path);
}else{
pathList.remove(0);
pathList.add(path);
}
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0 ;i < pathList.size();i++){
stringBuilder.append(pathList.get(i));
}
savePath.setText(stringBuilder.toString());
}
在大疆的Demo程序中,选择采用存储磁盘的方式来获取是各帧。处理函数为Mainactivity类中screenShot(byte[] buf, String shotDir)方法在此方法中使用Android内置类YUVImage的compressToJpeg()方法以流的方式进行存储,存储路径通过shotDir传入。