Android camera saves photos in Portrait Mode

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.