在常规的拍照方法在特殊的样机上异常时(有预览,take picture无异常信息,无回掉,甚至各大拍照软件也无法通过take picture 拍照时)找了一种折中的方式。通过
camera 的 PreviewCallback 回调的每一帧的数据,保存为image。主要问题是 会调过来的byte[] 格式为YUV420,需要一步转化。这样拍出的照片会比较小,因为是预览帧过来的。实属无奈。
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// TODO Auto-generated method stub
if (isFocus) {
isFocus = false;
try {
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;
YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 100, out);
byte[] bytes = out.toByteArray();
String strPath = Environment.getExternalStorageDirectory().toString() + "/Test/photos/";
File ff = new File(strPath);
if (!ff.exists()) {
ff.mkdirs();
}
String fileName = System.currentTimeMillis() + ".jpg";
FileOutputStream bos = new FileOutputStream(new File(strPath + fileName));
bos.write(bytes);
bos.flush();
bos.close();
handler.obtainMessage(MSG_GET_PIC, strPath + fileName).sendToTarget();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}