Solve Samsung phone photos after photo rotation.

Source: Internet
Author: User

Recent projects in the face of a Samsung phone photos, the picture will automatically rotate, should be the function of the Samsung internal system, and then need to do not let him rotate, found the method.

The principle is to get to the picture, judge its rotation angle, and then rotate back accordingly.

In the return result of the photo, get the path to the picture.

Path = FilePath + fileName; Path is the route returned by the photo
File File = new file (path);
int degree = Readpicturedegree (File.getabsolutepath ());
Bitmap smallbitmap=fileutils.compresssize (Path, 800, 800);
Smallbitmap = Rotaingimageview (degree, smallbitmap);
Saveallpath = Fileutils.savebitmap (Smallbitmap, System.currenttimemillis () + (Math.random () * 10000) + ". png");// Mass compression and storage of path
Imageitem Takephoto = new Imageitem ();
Takephoto.setbitmap (SMALLBITMAP);
Takephoto.setimagepath (Saveallpath);
Bimp.tempSelectBitmap.add (Takephoto);

/**

* Size Compression
* @param path Image Absolute
* @return degree angle of rotation
*/

public static Bitmap compresssize (final String path,final int w,final int h) {
Looper.prepare ();

TODO auto-generated Method Stub
Bitmapfactory.options opts = new Bitmapfactory.options ();
Set to Ture to get only the picture size
Opts.injustdecodebounds = true;//read-only edge, not read content
Opts.inpreferredconfig = Bitmap.Config.ARGB_8888;
Return to Empty
Bitmapfactory.decodefile (path, opts);
int width = opts.outwidth;
int height = opts.outheight;
To determine the suffix name
String suffix = "";
Compressformat format = null;
if (Path.endswith (". jpg")) {
suffix = ". jpg";
format = Compressformat.jpeg;
} else if (Path.endswith (". jpeg")) {
suffix = ". jpeg";
format = Compressformat.jpeg;
} else if (Path.endswith (". png")) {
suffix = ". png";
format = Compressformat.png;
} else {
suffix = ". jpg";
format = Compressformat.jpeg;
}
float ScaleWidth = 0.f, ScaleHeight = 0.F;
if (Width > W | | height > h) {//If wider than passed in width or height greater than the incoming height
Scaling
ScaleWidth = ((float) width)/w;
ScaleHeight = ((float) height)/h;
}
Opts.injustdecodebounds = false;
Maximum height and width after zooming
Float scale = Math.max (ScaleWidth, ScaleHeight);
opts.insamplesize = (int) scale;//Here is the last width and height value
Bitmap bmaprotate = bitmapfactory.decodefile (path, opts);
if (bmaprotate!=null) {
return bmaprotate;
}
return null;
}


/**

* Read Picture properties: Angle of rotation
* @param path Image Absolute
* @return degree angle of rotation
*/
public static int Readpicturedegree (String path) {
int degree = 0;
try {
Exifinterface exifinterface = new Exifinterface (path);
int orientation = Exifinterface.getattributeint (exifinterface.tag_orientation, exifinterface.orientation_normal);
Switch (orientation) {
Case EXIFINTERFACE.ORIENTATION_ROTATE_90:
degree = 90;
Break
Case EXIFINTERFACE.ORIENTATION_ROTATE_180:
degree = 180;
Break
Case EXIFINTERFACE.ORIENTATION_ROTATE_270:
degree = 270;
Break
}
} catch (IOException e) {
E.printstacktrace ();
}
return degree;
}
/*
*
* Rotate Picture
* */
public static Bitmap Rotaingimageview (int angle, Bitmap Bitmap) {
Rotate picture Action
Matrix matrix = new Matrix ();
;
Matrix.postrotate (angle);
System.out.println ("angle2=" + angle);
Create a new picture
Bitmap Resizedbitmap = Bitmap.createbitmap (Bitmap, 0, 0,
Bitmap.getwidth (), Bitmap.getheight (), Matrix, True);
return resizedbitmap;

}

/**
* Store picture, return path
* @param BM
* @param picname
* @return
*/
public static string Savebitmap (Bitmap BM, string picname) {
try {
if (!isfileexist (Picname)) {
File Tempf = Createsddir ("");
}
File F = new file (Sdpath, picname);


if (f.exists ()) {
F.delete ();


}
FileOutputStream out = new FileOutputStream (f);
Bm.compress (Bitmap.CompressFormat.PNG, N, out);
Out.flush ();
Out.close ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}


return Sdpath + picname;
}

Solve Samsung phone photos after photo rotation.

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.