public static String format(String format, Object... args) {
return new Formatter().format(format, args).toString();
}
public static boolean hasDebugFlag() {
String path = Environment.getExternalStorageDirectory() + "/Log/debug"; //文件路径
FileWriter writer = null;
try {
File file = new File(path);
return file.exists();
}catch (Exception e) {
}
return false;
}
public static void removeDebugFlag() {
String path = Environment.getExternalStorageDirectory() + "/Log/debug"; //文件路径
FileWriter writer = null;
try {
File file = new File(path);
if(file.exists()) {
file.delete();
}
}catch (Exception e) {
}
}
public static void writeLog(String fileName,String content) {
String path = Environment.getExternalStorageDirectory() + "/Log/"; //文件路径
FileWriter writer = null;
try {
File file = new File(path);
if (!file.exists()) { //没有创建文件夹则创建
file.mkdirs();
}
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
writer = new FileWriter(path + fileName, true);
writer.write(dateString + " " + content + "\r\n");
writer.flush();
if (writer != null) {
//关闭流
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean saveBitmap(Bitmap bitmap, Long id) {
if (bitmap == null)
return false;
String path = Environment.getExternalStorageDirectory() + "/Log/";
FileOutputStream fos = null;
try {
File f = new File(path +id.toString() + ".png");
if(f.exists()) {
f.delete();
}
f.createNewFile();
fos = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
public void dumpFrame(ImiDevice.ImiFrame nextFrame, Long id) {
try{
ByteBuffer frameData = nextFrame.getData();
int width = nextFrame.getWidth();
int height = nextFrame.getHeight();
frameData.position(0);
int length = frameData.remaining();
byte[] buf = new byte[length];
frameData.get(buf);
Bitmap save = com.commaai.face.util.Utils.RGB2Bitmap(buf, width, height);
saveBitmap(save, id);
}catch (Exception e) {
writeLog("brokenflow.txt", "failed to save bitmap");
}
}
使用逻辑
if(hasDebugFlag()) {
dumpcnt = 20;
removeDebugFlag();
}
if(dumpcnt > 0) {
dumpcnt--;
total++;
dumpFrame(nextFrame, total);
}