Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * This software and related documentation ("CK-Telecom Software") are
 * protected under relevant copyright laws. The information contained herein
 * is confidential and proprietary to CK-Telecom. and/or its licensors.
 * Without the prior written permission of CK-Telecom. and/or its licensors,
 * any reproduction, modification, use or disclosure of CK-Telecom Software,
 * and information contained herein, in whole or in part, shall be strictly              
 * prohibited.
 * CK-Telecom (C) 2012. All rights reserved.
 */

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileOutputStream;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.util.Log;

public class Main {
    private static String TAG = "HaaUtil";
    private static boolean isTag = true;

    public static Bitmap getSCBitmap(Bitmap bitmap, File file, float srcW, float srcH, float destW, float destH,
            int quality, int fileSize) {
        Bitmap bmp = null;

        bmp = getScaleBitmap(bitmap, srcW, srcH, destW, destH);
        bmp = getCompressBitmap(bmp, file, quality, fileSize);

        return bmp;
    }

    public static Bitmap getScaleBitmap(File file, int destW, int destH) {

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getPath(), opts);

        opts.inSampleSize = getSampleSize(opts.outWidth, opts.outHeight, destW, destH);
        opts.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(file.getPath(), opts);
        return bmp;
    }

    public static Bitmap getScaleBitmap(Bitmap bitmap, float srcW, float srcH, float destW, float destH) {
        Bitmap bmp = null;
        float orgW = 0;
        float orgH = 0;

        if (srcW <= destW && srcH <= destH) {
            return bitmap;
        }

        if ((srcW / srcH) > (destW / destH)) {
            orgW = destW;
            orgH = (orgW * srcH) / srcW;
        } else {
            orgH = destH;
            orgW = (orgH * srcW) / srcH;
        }

        bmp = Bitmap.createScaledBitmap(bitmap, (int) orgW, (int) orgH, false);
        return bmp;
    }

    public static Bitmap getCompressBitmap(Bitmap bitmap, File file, int quality, int fileSize) {
        Bitmap bmp = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        boolean result = bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
        LOG("getCompressBitmap result: " + result);
        try {
            if (file != null) {
                if (result) {
                    byte[] bt = bos.toByteArray();

                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(bt);
                    fos.close();

                    LOG("file.length(): " + file.length());

                    if (file.length() > fileSize) {
                        bmp = getCompressBitmap(bmp, file, (int) (quality * 0.8), fileSize);
                    } else {
                        bmp = BitmapFactory.decodeFile(file.getPath());
                    }
                }

            } else {
                bmp = BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.size());
            }
            bos.close();
        } catch (Exception e) {
            LOG("getCompressBitmap result: e" + e.toString());
            e.printStackTrace();
            return null;
        }
        return bmp;
    }

    public static int getSampleSize(int srcW, int srcH, int desW, int desH) {
        if (srcW > srcH) {
            if (desW >= srcW) {
                return 0;
            } else {
                return srcW / desW;
            }
        } else {
            if (desH >= srcH) {
                return 0;
            } else {
                return srcH / desH;
            }
        }
    }

    private static void LOG(String log) {
        if (isTag) {
            Log.d(TAG, log);
        }
    }
}