android - Scale and correct image orientation -
i trying scale size of image , @ same time create image orientation correct. both working if utilize them individually. need find way combining both. means, need scale downwards image , @ same time right it's orientation , display on imageview.
code image scaling:
public static bitmap decodesampledbitmapfromresource(string imagefilename,int reqwidth, int reqheight) { // first decode injustdecodebounds=true check dimensions final bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(imagefilename,options); // calculate insamplesize options.insamplesize = calculateinsamplesize(options, reqwidth, reqheight); // decode bitmap insamplesize set options.injustdecodebounds = false; homecoming bitmapfactory.decodefile(imagefilename, options); } /** * function calculate size of image */ public static int calculateinsamplesize(bitmapfactory.options options, int reqwidth, int reqheight) { // raw height , width of image final int height = options.outheight; final int width = options.outwidth; int insamplesize = 1; if (height > reqheight || width > reqwidth) { final int halfheight = height / 2; final int halfwidth = width / 2; // calculate largest insamplesize value powerfulness of 2 , keeps both // height , width larger requested height , width. while ((halfheight / insamplesize) > reqheight && (halfwidth / insamplesize) > reqwidth) { insamplesize *= 2; } } homecoming insamplesize; } here code correcting orientation:
/** * function manage orientation of image */ public static bitmap getcorrectlyorientedimage(context context, uri photouri) throws ioexception { inputstream = context.getcontentresolver().openinputstream(photouri); bitmapfactory.options dbo = new bitmapfactory.options(); dbo.injustdecodebounds = true; bitmapfactory.decodestream(is, null, dbo); is.close(); int rotatedwidth, rotatedheight; int orientation = getorientation(context, photouri); if (orientation == 90 || orientation == 270) { rotatedwidth = dbo.outheight; rotatedheight = dbo.outwidth; } else { rotatedwidth = dbo.outwidth; rotatedheight = dbo.outheight; } bitmap srcbitmap; = context.getcontentresolver().openinputstream(photouri); if (rotatedwidth > max_image_dimension || rotatedheight > max_image_dimension) { float widthratio = ((float) rotatedwidth) / ((float) max_image_dimension); float heightratio = ((float) rotatedheight) / ((float) max_image_dimension); float maxratio = math.max(widthratio, heightratio); // create bitmap file bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = (int) maxratio; srcbitmap = bitmapfactory.decodestream(is, null, options); } else { srcbitmap = bitmapfactory.decodestream(is); } is.close(); /* * if orientation not 0 (or -1, means don't know), * have rotation. */ if (orientation > 0) { matrix matrix = new matrix(); matrix.postrotate(orientation); srcbitmap = bitmap.createbitmap(srcbitmap, 0, 0, srcbitmap.getwidth(),srcbitmap.getheight(), matrix, true); } homecoming srcbitmap; } /** * function orientation of image */ public static int getorientation(context context, uri photouri) { /* it's on external media. */ cursor cursor = context.getcontentresolver().query(photouri,new string[] { mediastore.images.imagecolumns.orientation }, null, null, null); if (cursor.getcount() != 1) { homecoming -1; } cursor.movetofirst(); homecoming cursor.getint(0); } please suggest me way combining both.
try this
public static bitmap decodefile(file f) { seek { // decode image size bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodestream(new fileinputstream(f), null, o); // find right scale value. should powerfulness of 2. final int required_size = 70; int width_tmp = o.outwidth, height_tmp = o.outheight; int scale = 1; while (true) { if (width_tmp / 2 < required_size || height_tmp / 2 < required_size) break; width_tmp /= 2; height_tmp /= 2; scale++; } // decode insamplesize bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize = scale; homecoming bitmapfactory.decodestream(new fileinputstream(f), null, o2); } grab (filenotfoundexception e) { } homecoming null; } public static bitmap getcorrectbitmap(bitmap bitmap, string filepath) { exifinterface ei; seek { ei = new exifinterface(filepath); int orientation = ei.getattributeint(exifinterface.tag_orientation, exifinterface.orientation_normal); switch (orientation) { case exifinterface.orientation_rotate_90: homecoming rotateimage(bitmap, 90); case exifinterface.orientation_rotate_180: homecoming rotateimage(bitmap, 180); case exifinterface.orientation_rotate_270: homecoming rotateimage(bitmap, 270); } } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } homecoming bitmap; } android
No comments:
Post a Comment