zip Bitmap Image To 960 by 540 - Android Graphics

Android examples for Graphics:Bitmap Resize

Description

zip Bitmap Image To 960 by 540

Demo Code



//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.text.TextPaint;

public class Main {
  public static final int NormHigth = 960;
  public static final int MaxpOffset = 40;
  public static final int quality = 90;
  @SuppressLint("SimpleDateFormat")
  public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  public static boolean zipImageTo960x540(Bitmap mBitmap, int rotat, Rect rectbitmap, long time, String path,
      String img) {/*  w  ww  . j ava2  s. c om*/
    if (null == mBitmap) {
      return false;
    }
    FileOutputStream fos = null;
    Bitmap bitmap = null;
    try {
      if (mBitmap.getWidth() < 540 || mBitmap.getHeight() < 960) {
        float magnifywidth = ((float) 540.0 / mBitmap.getWidth());
        float magnifyheight = ((float) 960 / mBitmap.getHeight());
        float magnify = Math.max(magnifywidth, magnifyheight);
        Matrix matrix = new Matrix();

        matrix.postScale(magnify, magnify);

        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
      }

      File file = new File(path);
      if (!file.exists()) {
        file.mkdirs();
      }
      Matrix m = new Matrix();
      if (mBitmap.getWidth() - NormHigth > MaxpOffset || mBitmap.getHeight() - NormHigth > MaxpOffset) {

        float sx = 0f;
        if (mBitmap.getWidth() > mBitmap.getHeight()) {
          sx = (float) (960.0 / mBitmap.getWidth());
        } else {
          sx = (float) (960.0 / mBitmap.getHeight());
        }
        m.postScale(sx, sx);
        if (rotat == 90) {
          mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, true);
        }
      }
      if (rotat != 90) {
        m.preRotate(rotat - 90, ((float) mBitmap.getWidth() / 2), ((float) mBitmap.getHeight() / 2));
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, true);

      }

      Canvas canvas = new Canvas(mBitmap);
      TextPaint paint = new TextPaint();
      paint.setTextSize(20f);
      paint.setColor(Color.RED);
      canvas.drawText("asdf", mBitmap.getWidth() - 210, mBitmap.getHeight() - 10, paint);
      canvas.save();

      file = new File(path, img);
      fos = new FileOutputStream(file);
      mBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);

    } catch (Exception e) {
      e.printStackTrace();
      return false;
    } catch (Error e) {
      e.printStackTrace();
      return false;
    } finally {
      if (null != fos) {
        try {
          fos.flush();
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }

      }
      if (null != mBitmap) {
        mBitmap.recycle();
        mBitmap = null;
      }
      if (null != bitmap) {
        bitmap.recycle();
        bitmap = null;
      }

    }

    return true;
  }

  public static Bitmap createBitmap(String path, int w, int h) {
    try {
      BitmapFactory.Options opts = new BitmapFactory.Options();
      opts.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(path, opts);
      int srcWidth = opts.outWidth;
      int srcHeight = opts.outHeight;
      int destWidth = 0;
      int destHeight = 0;

      double ratio = 0.0;
      if (srcWidth < w || srcHeight < h) {
        ratio = 0.0;
        destWidth = srcWidth;
        destHeight = srcHeight;
      } else if (srcWidth > srcHeight) {
        ratio = (double) srcWidth / w;
        destWidth = w;
        destHeight = (int) (srcHeight / ratio);
      } else {
        ratio = (double) srcHeight / h;
        destHeight = h;
        destWidth = (int) (srcWidth / ratio);
      }
      BitmapFactory.Options newOpts = new BitmapFactory.Options();
      newOpts.inSampleSize = (int) ratio + 1;
      newOpts.inJustDecodeBounds = false;
      newOpts.outHeight = destHeight;
      newOpts.outWidth = destWidth;

      return BitmapFactory.decodeFile(path, newOpts);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}

Related Tutorials