Android圖片操作工具類

來源:互聯網
上載者:User

標籤:

package com.aliyun.oss.ossdemo;import android.app.Activity;import android.app.AlertDialog;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Handler;import android.os.Looper;import android.os.Message;import android.util.Log;import android.widget.Button;import android.widget.ImageView;import android.widget.ProgressBar;import android.widget.TextView;import android.widget.Toast;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;/** * Created by yjs on 2015/12/7 0007. * 完成顯示圖片操作 */public class ImageDisplayer {    private ImageView imageView;    private int height;    private int width;    public ImageDisplayer(ImageView imageView) {        this.imageView = imageView;    }    public ImageDisplayer(int height, int width) {        this.height = height;        this.width = width;    }    public static byte[] getBytesFromStream(InputStream stream) throws IOException {        {            ByteArrayOutputStream outStream = new ByteArrayOutputStream();            byte[] buffer = new byte[1024];            int len = 0;            while ((len = stream.read(buffer)) != -1) {                outStream.write(buffer, 0, len);            }            outStream.close();            return outStream.toByteArray();        }    }    //計算圖片縮放比例    public static int calculateInSampleSize(            BitmapFactory.Options options, int reqWidth, int reqHeight) {        // Raw height and 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 the largest inSampleSize value that 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;            }        }        return inSampleSize;    }    //根據ImageView的大小自動縮放圖片    public Bitmap autoResizeFromLocalFile(String picturePath) throws IOException {        // First decode with inJustDecodeBounds=true to check dimensions        final BitmapFactory.Options options = new BitmapFactory.Options();        options.inJustDecodeBounds = true;        BitmapFactory.decodeFile(picturePath, options);        // Calculate inSampleSize        int h = height;        int w = 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 = 10;        Log.d("SampleSize", String.valueOf(options.inSampleSize));        // Decode bitmap with inSampleSize set        options.inJustDecodeBounds = false;        return BitmapFactory.decodeFile(picturePath, options);    }    public Bitmap autoResizeFromBytes(byte[] data) {        // First decode with inJustDecodeBounds=true to check dimensions        final BitmapFactory.Options options = new BitmapFactory.Options();        options.inJustDecodeBounds = true;        BitmapFactory.decodeByteArray(data, 0, data.length, options);        int h = height;        int w = 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 = 10;        Log.d("SampleSize", String.valueOf(options.inSampleSize));        // Decode bitmap with inSampleSize set        options.inJustDecodeBounds = false;        return BitmapFactory.decodeByteArray(data, 0, data.length, options);    }    //根據ImageView大小自動縮放圖片    public Bitmap autoResizeFromStream(InputStream stream) throws IOException {        byte[] data = getBytesFromStream(stream);        return autoResizeFromBytes(data);    }    public Bitmap autoResizeFromBitmap(Bitmap bm) {        // First decode with inJustDecodeBounds=true to check dimensions        final BitmapFactory.Options options = new BitmapFactory.Options();        options.outHeight = bm.getHeight();        options.outWidth = bm.getWidth();        int h = height;        int w = width;        if (imageView != null) {            h = imageView.getHeight();            w = imageView.getWidth();        }        int 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));        if (inSampleSize == 1) {            return bm;        }        else {            return Bitmap.createScaledBitmap(bm, bm.getWidth() / inSampleSize, bm.getHeight() / inSampleSize, true);        }    }}

Android圖片操作工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.