最近一直在做一个扫描的项目,用zxing实现二维码的扫描,需求要实现拍照并且扫描名片,发现拍照出来的照片是旋转的。一直找原因找不到,在国外一个论坛看到说是camera获取data数据是横向的,没有经过处理所以显示就是旋转的。想了好久还是不会实现data的旋转,索性就将bitmap进行旋转以达到正面显示的效果吧。下面看代码:
Camera.PictureCallback myJpegCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap_cc = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix m = new Matrix();
m.setRotate(90,(float) bitmap_cc.getWidth() / 2, (float) bitmap_cc.getHeight() / 2);
final Bitmap bm = Bitmap.createBitmap(bitmap_cc, 0, 0, bitmap_cc.getWidth(), bitmap_cc.getHeight(), m, true);
String name = new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
File file = new File("/storage/emulated/0/DCIM/Camera_scanning/");
file.mkdirs();
File newFile = new File(file, name);
String fileName = "/storage/emulated/0/DCIM/Camera_scanning/" + name;
try {
FileOutputStream outputStream = new FileOutputStream(newFile);
bm.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("MainActivity", "data.length:" + data.length);
Log.d("MainActivity", fileName);
}
};