Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import android.graphics.Paint;

public class Main {
    private static final float PHOTO_BORDER_WIDTH = 3.0f;
    public static final String SDCARD_DIR = "/sdcard/mid/";
    private static final Paint sStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

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

    public static Bitmap scaleAndFrame(Bitmap bitmap, int width, int height) {
        final int bitmapWidth = bitmap.getWidth();
        final int bitmapHeight = bitmap.getHeight();

        final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight);

        final int scaledWidth = (int) (bitmapWidth * scale);
        final int scaledHeight = (int) (bitmapHeight * scale);

        final Bitmap decored = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
        final Canvas canvas = new Canvas(decored);

        final int offset = (int) (PHOTO_BORDER_WIDTH / 2);
        sStrokePaint.setAntiAlias(false);
        canvas.drawRect(offset, offset, scaledWidth - offset - 1, scaledHeight - offset - 1, sStrokePaint);
        sStrokePaint.setAntiAlias(true);

        return decored;

    }
}