When using camera for android, you may encounter two problems: orientation during camera preview, and orientation 90 degrees after takePicture.
First, solve the orientation problem of the first preview. First, in android2.2 and later sdk versions, the orientation of camera uses landscape. If the screenOrientation of your activity is set to landscape, this problem does not occur. Second, if your activity must use portrait, you can call camera. call the following method after the open () method to solve this problem,
[Java]
Camera. setDisplayOrientation (90 );
If your sdk is earlier than 2.2, you can solve the problem as follows:
[Java]
Method rotateMethod = android. hardware. Camera. class. getMethod ("setDisplayOrientation", int. class );
RotateMethod. invoke (camera, 90 );
The following is how to solve the problem of saving the Portrait mode to rotate a piece 90 degrees.
The following program is executed in the onPictureTaken method.
[Java]
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inSampleSize = 6;
Options. inDither = false;
Options. inPurgeable = true;
Options. ininputretriable = true;
[Java]
<Span style = "white-space: pre"> </span> options. inTempStorage = new byte [32*1024];
[Java]
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Bitmap bMap;
BMap = BitmapFactory. decodeByteArray (imgData [0], 0, imgData [0]. length, options );
If (bMap. getHeight () <bMap. getWidth ()){
Orientation = 90;
} Else {
Orientation = 0; www.2cto.com
}
Bitmap bMapRotate;
If (orientation! = 0 ){
Matrix matrix = new Matrix ();
Matrix. postRotate (orientation );
BMapRotate = Bitmap. createBitmap (bMap, 0, 0, bMap. getWidth (),
BMap. getHeight (), matrix, true );
} Else
BMapRotate = Bitmap. createScaledBitmap (bMap, bMap. getWidth (),
BMap. getHeight (), true );
FileOutputStream out;
Try {
File imgFile = new File ("/xxxx/xxx/snap.jpeg ");
Out = new FileOutputStream (imgFile );
BMapRotate. compress (Bitmap. CompressFormat. JPEG, 90, out );
If (bMapRotate! = Null ){
BMapRotate. recycle ();
BMapRotate = null;
}
<Span style = "white-space: pre"> </span> camera. startPreview ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
}
Author: hundsong