Example usage for android.graphics BitmapFactory decodeFile

List of usage examples for android.graphics BitmapFactory decodeFile

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeFile.

Prototype

public static Bitmap decodeFile(String pathName) 

Source Link

Document

Decode a file path into a bitmap.

Usage

From source file:Main.java

public static Bitmap cropJpgFile(String inFl, String outFl) throws IOException {
    Bitmap bmpIn = BitmapFactory.decodeFile(inFl);
    Bitmap bmOverlay = Bitmap.createBitmap(bmpIn.getWidth(), bmpIn.getHeight(), Bitmap.Config.ARGB_8888);
    Paint p = new Paint();
    p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    Canvas c = new Canvas(bmOverlay);
    c.drawBitmap(bmpIn, 0, 0, null);//from w w w  .ja  va  2  s.c  o  m
    //c.drawRect(30, 30, 100, 100, p);
    File fileOut = new File(outFl);
    FileOutputStream out = new FileOutputStream(fileOut);
    bmOverlay = drawTextToBitmap(bmOverlay, "Image Viewer");
    bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, out);
    return bmOverlay;
}

From source file:Main.java

/**
 * Adjust the photo orientation//from w w w. ja  v  a 2 s .c  o m
 * @param pathToFile
 * @return
 */
public static Bitmap adjustPhotoOrientation(String pathToFile) {
    try {
        Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
        ExifInterface exif = new ExifInterface(pathToFile);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;

        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        }
        if (rotate != 0) {
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            // Setting pre rotate
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            // Rotating Bitmap & convert to ARGB_8888, required by tess
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            return bitmap;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap readImageFile(String fileName) {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }// ww  w  . j  a v a  2s.c om
    File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + fileName);
    if (!file.exists()) {
        return null;
    }
    return BitmapFactory.decodeFile(fileName);
}

From source file:Main.java

public static void compressFileIfNeeded(String filePath) {
    File f = new File(filePath);

    Bitmap bitmap;/*w  w  w  .j  av a  2  s.  c om*/
    bitmap = BitmapFactory.decodeFile(filePath);
    int MAX_IMAGE_SIZE = 1000 * 1024;
    int streamLength = (int) f.length();
    if (streamLength > MAX_IMAGE_SIZE) {
        int compressQuality = 105;
        ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
        while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
            try {
                bmpStream.flush();//to avoid out of memory error
                bmpStream.reset();
            } catch (IOException e) {
                e.printStackTrace();
            }
            compressQuality -= 5;
            bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
            byte[] bmpPicByteArray = bmpStream.toByteArray();
            streamLength = bmpPicByteArray.length;
            Log.d("test upload", "Quality: " + compressQuality);
            Log.d("test upload", "Size: " + streamLength);
        }

        FileOutputStream fo;

        try {
            f.delete();
            f = new File(filePath);
            fo = new FileOutputStream(f);
            fo.write(bmpStream.toByteArray());
            fo.flush();
            fo.close();
            ExifInterface exif = new ExifInterface(f.getAbsolutePath());
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, "6");
            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Bitmap getBitmapFromUrl(String imageUrl) {
    Bitmap bmp = null;//ww  w .j  av a  2 s .c o  m
    try {
        if (!TextUtils.isEmpty(imageUrl) && imageUrl.startsWith("http")) {
            URL url = new URL(imageUrl);
            bmp = BitmapFactory.decodeStream(url.openStream());
        } else {
            bmp = BitmapFactory.decodeFile(imageUrl);
        }
    } catch (Throwable ex) {

    }
    return bmp;
}

From source file:Main.java

/**
 * get a bitmap by giving a file location
 * @param fileName/*from w  w  w .j  a  v a 2  s . c  o  m*/
 * @return
 */
public static Bitmap getImgFromLocation(String fileName) {
    File imgFile = new File(fileName);

    Bitmap myBitmap = null;

    //shows an image on the screen
    if (imgFile.exists()) {

        myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        Log.i(TAG, "file saved");

    } else
        Log.i(TAG, "file not found");

    return myBitmap;
}

From source file:Main.java

public static Bitmap roundCornerFromFile(String filePath, int pixels) {
    try {/*ww  w.  jav  a 2  s  .c om*/
        Bitmap bitmap = BitmapFactory.decodeFile(filePath);
        if (bitmap == null)
            return null;
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        Canvas canvas = new Canvas(output);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        bitmap.recycle();
        bitmap = null;
        return output;
    } catch (OutOfMemoryError e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static SpannableString StringToBitMap(Context context, String msg) {
    SpannableString ss = new SpannableString(msg);
    Pattern p = Pattern.compile("/mnt/sdcard/.+?\\.\\w{3}");
    Matcher m = p.matcher(msg);//from  w  w w.  ja v  a 2 s. co m
    while (m.find()) {
        Bitmap bitmap = BitmapFactory.decodeFile(m.group());
        ImageSpan imageSpan = new ImageSpan(context, bitmap);
        ss.setSpan(imageSpan, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return ss;
}

From source file:Main.java

public static byte[] bitmapToByte(String path) {
    File file = new File(path);
    if (!file.exists()) {
        return null;
    }/*from  w ww .j  av  a2s  . c o m*/
    Bitmap bitmap = BitmapFactory.decodeFile(path);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    return bos.toByteArray();
}

From source file:Main.java

public static Bitmap getSamlBitmapFromFill(String fileName, int width, int height) {
    if (fileName.split("/").length <= 1) {
        fileName = SDCARD_DIR + fileName;
    }//from  w  w  w  . jav  a 2  s  .  com
    Bitmap srcBitmap = BitmapFactory.decodeFile(fileName);
    if (srcBitmap == null) {
        return null;
    }
    Bitmap destBitmap = scaleAndFrame(srcBitmap, width, height);
    return destBitmap;
}