Resize Photo : Image « 2D Graphics « Android






Resize Photo

   

/*
 * Copyright (C) 2011 The Pluroium Development Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//package org.pluroid.pluroium;

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.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.preference.PreferenceManager;

class Utils {

    private static final String TAG = "Utils";
    
    private static final float MEDIUM_SIZE = 640.0f;    
    private static final float SMALL_SIZE = 240.0f;
    private static final int IO_BUFFER_SIZE = 4 * 1024;

    private static File getCacheDirectory(Context context, String dirname) {
        
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean useExternal = false;
        File directory;

        if (useExternal && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            directory = Environment.getExternalStorageDirectory();
        } else {
            directory = context.getCacheDir();
        }

        File subdir = new File(directory, "pluroid");
        if (!subdir.exists()) {
            subdir.mkdirs();
        }

        return new File(subdir, dirname);
    }
    
    public static File ensureCache(Context context, String dirname) throws IOException {
        File cacheDirectory = getCacheDirectory(context, dirname);
        if (!cacheDirectory.exists()) {
            cacheDirectory.mkdirs();
            new File(cacheDirectory, ".nomedia").createNewFile();
        }   
        return cacheDirectory;
    }

    private static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] b = new byte[IO_BUFFER_SIZE];
        int read;
     
        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
        }
    }
    
    public static File resizePhoto(Context context, String filePath, String resize) throws IOException {
        BitmapFactory.Options option;
        Bitmap originalBitmap, resizedBitmap;
        
        File cacheDirectory = getCacheDirectory(context, "images");
        if (!cacheDirectory.exists()) {
            cacheDirectory.mkdirs();
            new File(cacheDirectory, ".nomedia").createNewFile();
        }
        
        File newFilePath = new File(cacheDirectory, System.currentTimeMillis() + ".jpg");
        
        option = new BitmapFactory.Options();
        option.inJustDecodeBounds = true;
        originalBitmap = BitmapFactory.decodeFile(filePath, option);
        int w = option.outWidth, h = option.outHeight;
        int rw, rh;
        float newSize = "no".equals(resize) ? 0.0f : "medium".equals(resize) ? MEDIUM_SIZE : SMALL_SIZE;
        
        if (newSize == 0.0f) {
            FileInputStream fis = new FileInputStream(filePath);
            FileOutputStream fos = new FileOutputStream(newFilePath);
            copy(fis, fos);
            fis.close();
            fos.close();
            return newFilePath;
        } else {
            if (w > h && w >= newSize) {
                rw = (int) newSize;
                rh = (int) (h * newSize / w);
            } else if (h > w && h >= newSize) {
                rh = (int) newSize;
                rw = (int) (w * newSize / h);
            } else {
                rh = h;
                rw = w;
            }
            
            int scale = 1;
            while (true) {
                if (w / 2 < newSize || h / 2 < newSize) {
                    break;
                }
                w /= 2;
                h /= 2;
                scale++;
            }
            option = new BitmapFactory.Options();
            option.inSampleSize = scale - 1;
            originalBitmap = BitmapFactory.decodeFile(filePath, option);
            resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, rw, rh, true);
            
            FileOutputStream fos = new FileOutputStream(newFilePath);
            resizedBitmap.compress(CompressFormat.JPEG, 100, fos);
            fos.close();
            
            return newFilePath;
        }
    }
}

   
    
    
  








Related examples in the same category

1.Capture Image
2.extends BaseAdapter to create Image adapter
3.extends BaseAdapter to create adapter for images
4.Capture Image and display
5.Load up the image's dimensions
6.Lazy Loading Image
7.Resize Image
8.Fit Image No Margin
9.To Rotate Texture Image
10.Image Resize
11.Create Image and resize Image
12.image To Byte
13.Image Loader
14.Save Image and Text to SD card
15.Scale and rotate Image
16.create Image