crop Jpg File - Android Graphics

Android examples for Graphics:JPEG Image

Description

crop Jpg File

Demo Code


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

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;

public class Main {
    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);
        //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   www.j  av a  2 s .co m*/

    public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
        //Resources resources = gContext.getResources();
        //float scale = resources.getDisplayMetrics().density;

        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        bitmap = bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);
        // new antialised Paint
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // text color - #3D3D3D
        paint.setColor(Color.rgb(61, 61, 61));
        // text size in pixels
        paint.setTextSize((int) (21)); //* scale));
        // text shadow
        paint.setShadowLayer(2f, 1f, 1f, Color.WHITE);
        // draw text to the Canvas center
        //Rect bounds = new Rect();
        //paint.getTextBounds(gText, 0, gText.length(), bounds);
        int x = bitmap.getWidth() - 150;//bounds.width()) - 150;
        int y = bitmap.getHeight() - 27;//bounds.height()) - 30;
        // fill
        canvas.drawRect(x, y, x + 150, y + 27, paint);
        canvas.drawText(gText, x, y + 20, paint);
        return bitmap;
    }
}

Related Tutorials