Android图片操作工具类

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);
        }
    }
}
时间: 2024-11-02 01:03:10

Android图片操作工具类的相关文章

Android文件操作工具类(转)

Android文件操作工具类(转) 2014/4/3 18:13:35  孤独的旅行家  博客园 这个工具类包含Android应用开发最基本的几个文件操作方法,也是我第一次发博客与大家分享自己写的东西. 如果有什么补充或修改,欢迎大家提出宝贵的建议. import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;impo

一个好用的android图片压缩工具类

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">用了很久图片压缩,之前人们一直使用google的官方图片压缩方法</span> final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = tr

Android 文件操作工具类

package cn.ibabyzone.library; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.app.Activity; import android.os.En

Android设备内存和SD卡操作工具类

package cc.c; import java.io.File; import java.util.List; import android.os.StatFs; import java.io.FileReader; import java.io.IOException; import java.io.BufferedReader; import android.os.Environment; import android.content.Context; import android.ap

Android 中图可以用到的图片处理类 BitmapUtils

Android在实际开发中很多时候都要对图片进行一定的处理,这里总结的BitmapUtils 类包括一下几个功能: 1.Android图片倒影, 2.Android图片模糊处理, 3.Android图片圆角处理, 4.图片沿着y轴旋转一定角度, 5.Android给图片添加边框. 接下来就直接上代码了,代码中有一定的解释.直接哪来用就可以了. /* * @Title: BitmapUtils.java * @Copyright: Corporation. Ltd. Copyright 1998-

Android Handler 异步消息处理机制的妙用 创建强大的图片载入类

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38476887 ,本文出自[张鸿洋的博客] 近期创建了一个群.方便大家交流,群号:55032675 上一篇博客介绍了Android异步消息处理机制.假设你还不了解,能够看:Android 异步消息处理机制 让你深入理解 Looper.Handler.Message三者关系 . 那篇博客的最后,提出能够把异步消息处理机制不仅仅是在MainActivity中更新UI.能够用到别的地方

Android 中图能够用到的图片处理类 BitmapUtils

Android在实际开发中非常多时候都要对图片进行一定的处理,这里总结的BitmapUtils 类包含一下几个功能: 1.Android图片倒影, 2.Android图片模糊处理, 3.Android图片圆角处理, 4.图片沿着y轴旋转一定角度, 5.Android给图片加入边框. 6.将View 转换成bitmap 7.将大图片二次压缩 8.压缩图片获得缩略图 9.获得本地文件视频的缩略图 10.获得本地文件图片的缩略图 接下来就直接上代码了,代码中有一定的解释. 直接哪来用就能够了. </p

android greenDao SQLite数据库操作工具类使用

上一篇介绍了如何建立类生成工程,现在介绍如何使用. 以下是ExampleDaoGenerator工程代码,做了一些修改 /* * Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in com

Android 调节图片工具类

package com.base.changeimage; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; /** * 调节图