Android Open Source - ImageScanner Format Bitmap Utils






From Project

Back to project page ImageScanner.

License

The source code is released under:

Apache License

If you think the Android project ImageScanner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.scanner.utils;
/* ww w  .jav  a2 s. c o m*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.MediaStore;

public class FormatBitmapUtils {
  
  
  // drawable to file
  public static void drawableTofile(Context context, Drawable drawable) {
    MediaStore.Images.Media.insertImage(context.getContentResolver(), drawable2Bitmap(drawable),
        "title", "description");
  }

  // convert file to bitmap
  public static Bitmap file2Bitmap(String path) {
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(path);
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(fis);
    return bitmap;
  }
  
  // convert file to bitmap
  public static Drawable file2Drawable(String path) {
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(path);
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(fis);
    return new BitmapDrawable(bitmap);
    
  }

  // bytes to inputstream
  public static InputStream Byte2InputStream(byte[] b) {
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    return bais;
  }

  // ??InputStream?????byte[]
  public static 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;
  }

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

  // ??Bitmap?????InputStream
  public static 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;
  }

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

  // Drawable?????InputStream
  public static InputStream Drawable2InputStream(Drawable d) {
    Bitmap bitmap = drawable2Bitmap(d);
    return Bitmap2InputStream(bitmap);
  }

  // InputStream?????Drawable
  public static Drawable InputStream2Drawable(InputStream is) {
    Bitmap bitmap = InputStream2Bitmap(is);
    return bitmap2Drawable(bitmap);
  }

  // Drawable?????byte[]
  public static byte[] Drawable2Bytes(Drawable d) {
    Bitmap bitmap = drawable2Bitmap(d);
    return Bitmap2Bytes(bitmap);
  }

  // byte[]?????Drawable
  public static Drawable Bytes2Drawable(byte[] b) {
    Bitmap bitmap = Bytes2Bitmap(b);
    return bitmap2Drawable(bitmap);
  }

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

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

  // drawable to bitmap
  public static 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;
  }

  // Bitmap to Drawable
  public static Drawable bitmap2Drawable(Bitmap bitmap) {
    BitmapDrawable bd = new BitmapDrawable(bitmap);
    Drawable d = (Drawable) bd;
    return d;
  }
  // save bitmap to local
  public static void saveBitmap(String path , Bitmap bitmap){
    File file = new File(path);
    try {
      FileOutputStream fileOutputStream = new FileOutputStream(file);
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
      fileOutputStream.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  
}




Java Source Code List

com.atermenji.android.iconicdroid.IconicFontDrawable.java
com.atermenji.android.iconicdroid.icon.EntypoIcon.java
com.atermenji.android.iconicdroid.icon.EntypoSocialIcon.java
com.atermenji.android.iconicdroid.icon.FontAwesomeIcon.java
com.atermenji.android.iconicdroid.icon.Icon.java
com.atermenji.android.iconicdroid.icon.IconicIcon.java
com.atermenji.android.iconicdroid.util.TypefaceManager.java
com.example.imagescanner.GalleryFolderActivity.java
com.example.imagescanner.GalleryGridActivity.java
com.example.imagescanner.MainActivity.java
com.scanner.adapter.GalleryFolderListAdapter.java
com.scanner.adapter.GalleryGridAdapter.java
com.scanner.beans.GalleryFolderBean.java
com.scanner.beans.GridImageBean.java
com.scanner.utils.BitmapUtils.java
com.scanner.utils.ConstantsUtils.java
com.scanner.utils.DensityUtil.java
com.scanner.utils.FormatBitmapUtils.java
com.scanner.utils.ImageLoaderCacheHelper.java
com.scanner.utils.L.java
com.scanner.utils.ScannerUtils.java
com.scanner.utils.UIHelper.java