Android Picture Manipulation Tool class

Source: Internet
Author: User

 PackageCom.aliyun.oss.ossdemo;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapFactory;ImportAndroid.os.Handler;ImportAndroid.os.Looper;ImportAndroid.os.Message;ImportAndroid.util.Log;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;ImportAndroid.widget.ProgressBar;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportJava.io.ByteArrayOutputStream;ImportJava.io.IOException;ImportJava.io.InputStream;/** * Created by YJS on 2015/12/7 0007. * Finish displaying picture operation */ Public  class imagedisplayer {    PrivateImageView ImageView;Private intHeightPrivate intWidth Public Imagedisplayer(ImageView ImageView) { This. ImageView = ImageView; } Public Imagedisplayer(intHeightintwidth) { This. height = height; This. width = width; } Public Static byte[]Getbytesfromstream(InputStream Stream)throwsIOException {{Bytearrayoutputstream OutStream =NewBytearrayoutputstream ();byte[] buffer =New byte[1024x768];intLen =0; while(len = stream.read (buffer))! =-1) {outstream.write (buffer,0, Len); } outstream.close ();returnOutstream.tobytearray (); }    }//Calculate picture zoom ratio     Public Static int calculateinsamplesize(Bitmapfactory.options Options,intReqwidth,intReqheight) {//Raw height and width of image        Final intHeight = options.outheight;Final intwidth = options.outwidth;intInsamplesize =1;if(Height > Reqheight | | width > reqwidth) {Final intHalfheight = height/2;Final intHalfwidth = width/2;//Calculate The largest insamplesize value is a power of 2 and keeps both            //height and width larger than the requested height and width.             while(halfheight/insamplesize) > Reqheight && (halfwidth/insamplesize) > Reqwidth) {Insamplesize *=2; }        }returnInsamplesize; }//Automatically scale pictures according to the size of ImageView     PublicBitmapAutoresizefromlocalfile(String PicturePath)throwsIOException {//First decode with injustdecodebounds=true to check dimensions        FinalBitmapfactory.options Options =NewBitmapfactory.options (); Options.injustdecodebounds =true; Bitmapfactory.decodefile (PicturePath, Options);//Calculate insamplesize        inth = height;intw = width;if(ImageView! =NULL) {h = imageview.getheight ();        W = imageview.getwidth ();        } options.insamplesize = Calculateinsamplesize (Options, W, h); LOG.D ("ImageHeight", string.valueof (options.outheight)); LOG.D ("ImageWidth", string.valueof (options.outwidth)); LOG.D ("Height", string.valueof (h)); LOG.D ("Width", String.valueof (w));//options.insamplesize = ten;LOG.D ("SampleSize", string.valueof (options.insamplesize));//Decode bitmap with Insamplesize setOptions.injustdecodebounds =false;returnBitmapfactory.decodefile (PicturePath, Options); } PublicBitmapautoresizefrombytes(byte[] data) {//First decode with injustdecodebounds=true to check dimensions        FinalBitmapfactory.options Options =NewBitmapfactory.options (); Options.injustdecodebounds =true; Bitmapfactory.decodebytearray (data,0, data.length, Options);inth = height;intw = width;if(ImageView! =NULL) {h = imageview.getheight ();        W = imageview.getwidth ();        } options.insamplesize = Calculateinsamplesize (Options, W, h); LOG.D ("ImageHeight", string.valueof (options.outheight)); LOG.D ("ImageWidth", string.valueof (options.outwidth)); LOG.D ("Height", string.valueof (h)); LOG.D ("Width", String.valueof (w));//options.insamplesize = ten;LOG.D ("SampleSize", string.valueof (options.insamplesize));//Decode bitmap with Insamplesize setOptions.injustdecodebounds =false;returnBitmapfactory.decodebytearray (data,0, data.length, Options); }//Automatically scale pictures according to ImageView size     PublicBitmapAutoresizefromstream(InputStream Stream)throwsIOException {byte[] data = Getbytesfromstream (stream);returnAutoresizefrombytes (data); } PublicBitmapAutoresizefrombitmap(Bitmap BM) {//First decode with injustdecodebounds=true to check dimensions        FinalBitmapfactory.options Options =NewBitmapfactory.options ();        Options.outheight = Bm.getheight (); Options.outwidth = Bm.getwidth ();inth = height;intw = width;if(ImageView! =NULL) {h = imageview.getheight ();        W = imageview.getwidth (); }intInsamplesize = calculateinsamplesize (Options, W, h); LOG.D ("ImageHeight", string.valueof (options.outheight)); LOG.D ("ImageWidth", string.valueof (options.outwidth)); LOG.D ("Height", string.valueof (h)); LOG.D ("Width", String.valueof (w));if(Insamplesize = =1) {returnBm }Else{returnBitmap.createscaledbitmap (BM, bm.getwidth ()/Insamplesize, bm.getheight ()/Insamplesize,true); }    }}

Android Picture Manipulation Tool class

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.