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