现在的位置: 首页 > 移动开发 > 正文

【android】bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换

2018年09月25日 移动开发 ⁄ 共 10572字 ⁄ 字号 评论关闭

1.将view转为bitmap

 

public static Bitmap getBitmapFromView(View view)
{
    // Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    // Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    // Get the view's background
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        // has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    else
        // does not have background drawable, then draw white background on
        // the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    // return the bitmap
    return returnedBitmap;
}



2.将view转为bitmap

public static Bitmap viewToBitmap(View view)
{
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bm = view.getDrawingCache();
    return bm;
}




3.将xml转为bitmap

public static Bitmap convertBitmapFromXML(Context context, String clusterSize, Bitmap bm)
{
                                                                                                                                                                                                                                                                                                                                                                                        
    View layout = LayoutInflater.from(context).inflate(R.layout.estatecartlist_item, null);
    View bitmapView = layout.findViewById(R.id.estatecartlist_item_bitmap);
    TextView xml_text = (TextView) layout.findViewById(R.id.item_estatecart_tv_name);
    ImageView image = (ImageView) layout.findViewById(R.id.item_estatecart_iv_main);
    image.setImageBitmap(bm);
    xml_text.setText(clusterSize);
                                                                                                                                                                                                                                                                                                                                                                                        
    bitmapView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                                                                                                                                                                                                                                                                                                                                                                                        
    bitmapView.layout(0, 0, bitmapView.getMeasuredWidth(), bitmapView.getMeasuredHeight());
                                                                                                                                                                                                                                                                                                                                                                                        
    final Bitmap clusterBitmap = Bitmap.createBitmap(bitmapView.getMeasuredWidth(), bitmapView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
                                                                                                                                                                                                                                                                                                                                                                                        
    Canvas canvas = new Canvas(clusterBitmap);
    bitmapView.draw(canvas);
    return clusterBitmap;
}




==============参考资料===================
* 1.http://stackoverflow.com/questions/7200535/how-to-convert-views-to-bitmap
* 2.http://stackoverflow.com/questions/5536066/convert-view-to-bitmap-on-android/9595919#9595919
* 3.http://stackoverflow.com/questions/12402392/android-converting-xml-view-to-bitmap-without-showing-it





4.图片缩放与压缩

private Bitmap getimage(String srcPath)
{
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    // 开始读入图片,此时把options.inJustDecodeBounds 设回true了
    newOpts.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空
                                                                                                                                                                                                                                                                                                                                                                               
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
    float hh = 800f;// 这里设置高度为800f
    float ww = 480f;// 这里设置宽度为480f
    // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
    int be = 1;// be=1表示不缩放
    if (w > h && w > ww)
    {// 如果宽度大的话根据宽度固定大小缩放
        be = (int) (newOpts.outWidth / ww);
    }
    else if (w < h && h > hh)
    {// 如果高度高的话根据宽度固定大小缩放
        be = (int) (newOpts.outHeight / hh);
    }
    if (be <= 0)
        be = 1;
    newOpts.inSampleSize = be;// 设置缩放比例
    // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
    bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
    return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
}
// 图片质量压缩
private static Bitmap compressImage(Bitmap image)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
    int options = 100;
    while (baos.toByteArray().length / 1024 > 100)
    { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
        baos.reset();// 重置baos即清空baos
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中
        options -= 10;// 每次都减少10
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
    return bitmap;
}
// 图片按比例大小压缩
private static Bitmap comp(Bitmap image)
{
                                                                                                                                                                                                                                                                                                                                                                               
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    if (baos.toByteArray().length / 1024 > 1024)
    {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
        baos.reset();// 重置baos即清空baos
        image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 这里压缩50%,把压缩后的数据存放到baos中
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    // 开始读入图片,此时把options.inJustDecodeBounds 设回true了
    newOpts.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
    float hh = 800f;// 这里设置高度为800f
    float ww = 480f;// 这里设置宽度为480f
    // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
    int be = 1;// be=1表示不缩放
    if (w > h && w > ww)
    {// 如果宽度大的话根据宽度固定大小缩放
        be = (int) (newOpts.outWidth / ww);
    }
    else if (w < h && h > hh)
    {// 如果高度高的话根据宽度固定大小缩放
        be = (int) (newOpts.outHeight / hh);
    }
    if (be <= 0)
        be = 1;
    newOpts.inSampleSize = be;// 设置缩放比例
    // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
    isBm = new ByteArrayInputStream(baos.toByteArray());
    bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
    return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩
}




5.图片转为文件

public static boolean saveBitmap2file(Bitmap bmp, String filename)
{
    CompressFormat format = Bitmap.CompressFormat.JPEG;
    int quality = 100;
    OutputStream stream = null;
    try
    {
        stream = new FileOutputStream("/sdcard/" + filename);
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
                                                                                                                                                                                                                                                                                                                                                                      
    return bmp.compress(format, quality, stream);
}



6.屏幕截屏方法

// 获取当前屏幕bitmap,转换成bytes[] 调用 UI分享方法
public void printscreen_share(View v)
{
    View view1 = getWindow().getDecorView();
    Display display = getWindowManager().getDefaultDisplay();
    view1.layout(0, 0, display.getWidth(), display.getHeight());
    view1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(view1.getDrawingCache());
}




7.把Bitmap 转成 Byte

public static byte[] Bitmap2Bytes(Bitmap bm)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}



8.图片转为文件

public static boolean saveBitmap2file(Bitmap bmp)
{
    CompressFormat format = Bitmap.CompressFormat.PNG;
    int quality = 100;
    OutputStream stream = null;
    try
    {
        // 判断SDcard状态
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
        {
            // 错误提示
            return false;
        }
                                                                                                                                                                                                                                                                                                        
        // 检查SDcard空间
        File SDCardRoot = Environment.getExternalStorageDirectory();
        if (SDCardRoot.getFreeSpace() < 10000)
        {
            // 弹出对话框提示用户空间不够
            Log.e("Utils", "存储空间不够");
            return false;
        }
                                                                                                                                                                                                                                                                                                        
        // 在SDcard创建文件夹及文件
        File bitmapFile = new File(SDCardRoot.getPath() + FILE_PATH);
        bitmapFile.getParentFile().mkdirs();// 创建文件夹
        stream = new FileOutputStream(SDCardRoot.getPath() + FILE_PATH);// "/sdcard/"
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    return bmp.compress(format, quality, stream);
}




9.下载图片

public static Bitmap loadImage(String... params)
{
    InputStream is = null;
    Bitmap bitmap = null;
    try
    {
        URL url = new URL(params[0]);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        if (HttpURLConnection.HTTP_OK != conn.getResponseCode())
        {
            // 网络连接异常
            Log.e("", "loadImage连接异常");
            return null;
        }
                                                                                                                                                                                                                                                                           
        is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
                                                                                                                                                                                                                                                                           
    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (null != is)
        {
            try
            {
                is.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    return bitmap;
}



10.byte[]转换成Bitmap

public static Bitmap Bytes2Bitmap(byte[] b)
{
    if (b.length != 0)
    {
        return BitmapFactory.decodeByteArray(b, 0, b.length);
    }
    return null;
}



11.将字符串转换成Bitmap类型

public static Bitmap stringtoBitmap(String string)
{
    // 将字符串转换成Bitmap类型
    Bitmap bitmap = null;
    try
    {
        byte[] bitmapArray;
        bitmapArray = Base64.decode(string, Base64.DEFAULT);
        bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
                                                                                                                                                                                                                                             
    return bitmap;
}




12.将Bitmap转换成字符串

public static String bitmaptoString(Bitmap bitmap)
{
    // 将Bitmap转换成字符串
    String string = null;
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 100, bStream);
    byte[] bytes = bStream.toByteArray();
    string = Base64.encodeToString(bytes, Base64.DEFAULT);
    return string;
}



13.byte[]转为文件

public static File getFileFromBytes(byte[] b)
{
    BufferedOutputStream stream = null;
    File file = null;
    try
    {
        // 判断SDcard状态
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
        {
            // 错误提示
            return null;
        }
                                                                                                                                                                                                                       
        // 检查SDcard空间
        File SDCardRoot = Environment.getExternalStorageDirectory();
        if (SDCardRoot.getFreeSpace() < 10000)
        {
            // 弹出对话框提示用户空间不够
            Log.e("Utils", "存储空间不够");
            return null;
        }
                                                                                                                                                                                                                       
        // 在SDcard创建文件夹及文件
        File bitmapFile = new File(SDCardRoot.getPath() + FILE_PATH);
        bitmapFile.getParentFile().mkdirs();// 创建文件夹
                                                                                                                                                                                                                       
        FileOutputStream fstream = new FileOutputStream(bitmapFile);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (stream != null)
        {
            try
            {
                stream.close();
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }
        }
    }
    return file;
}




14.图片压缩

public static Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context)
{
                                                                                                                                                                                                        
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;
                                                                                                                                                                                                        
    int h = (int) (newHeight * densityMultiplier);
    int w = (int) (h * photo.getWidth() / ((double) photo.getHeight()));
                                                                                                                                                                                                        
    photo = Bitmap.createScaledBitmap(photo, w, h, true);
                                                                                                                                                                                                        
    return photo;
}


15.将byte[]转换成InputStream

public InputStream Byte2InputStream(byte[] b)
{
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    return bais;
}




16.将InputStream转换成byte[]

public byte[] InputStream2Bytes(InputStream is)
{
    String str = "";
    byte[] readByte = new byte[1024];
    int readCount = -1;
    try
    {
        while ((readCount = is.read(readByte, 0, 1024)) != -1)
        {
            str += new String(readByte).trim();
        }
        return str.getBytes();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return null;
}



17.将Bitmap转换成InputStream

public InputStream Bitmap2InputStream(Bitmap bm)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;
}




18.将Bitmap转换成InputStream

public InputStream Bitmap2InputStream(Bitmap bm, int quality)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;
}




19.将InputStream转换成Bitmap

public Bitmap InputStream2Bitmap(InputStream is)
{
    return BitmapFactory.decodeStream(is);
}



20.Drawable转换成InputStream

public InputStream Drawable2InputStream(Drawable d)
{
    Bitmap bitmap = this.drawable2Bitmap(d);
    return this.Bitmap2InputStream(bitmap);
}




21.InputStream转换成Drawable

public Drawable InputStream2Drawable(InputStream is)
{
    Bitmap bitmap = this.InputStream2Bitmap(is);
    return this.bitmap2Drawable(bitmap);
}


22.Drawable转换成byte[]

public byte[] Drawable2Bytes(Drawable d)
{
    Bitmap bitmap = this.drawable2Bitmap(d);
    return this.Bitmap2Bytes(bitmap);
}



23.byte[]转换成Drawable

public Drawable Bytes2Drawable(byte[] b)
{
    Bitmap bitmap = this.Bytes2Bitmap(b);
    return this.bitmap2Drawable(bitmap);
}



24.Drawable转换成Bitmap

public Bitmap drawable2Bitmap(Drawable drawable)
{
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    return bitmap;
}



25.Bitmap转换成Drawable

public Drawable bitmap2Drawable(Bitmap bitmap)
{
    BitmapDrawable bd = new BitmapDrawable(bitmap);
    Drawable d = (Drawable) bd;
    return d;
}

 

转自http://blog.14401.cn/post-156.html

抱歉!评论已关闭.