Android:缩小图片尺寸,并添加水印

本文介绍了一个用于照片大小优化并添加水印的算法实现,通过调整图像尺寸和应用时间戳水印,实现了在减少数据成本的同时保持高质量图片效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class AIS_PhotoResize {

//resize photos to reduce data cost for sending photos
public String ResizePhoto(String photoName)
{       
    //folder location for existing photos
    File oldFileRoot = new File(Environment.getExternalStorageDirectory() + File.separator + "AIS_Photos" + File.separator);
    File oldFile = new File(oldFileRoot, photoName);

    //folder location for resized photos
    File newFileRoot = new File(Environment.getExternalStorageDirectory() + File.separator + "AIS_Photos" + File.separator + "Resized" + File.separator);
    //ensure folder exist
    newFileRoot.mkdirs();
    File newFile = new File(newFileRoot, photoName);

    Bitmap b = null;
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(oldFile);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

        //The new size we want to scale to
        final int IMAGE_MAX_SIZE=800;

        int scale = 1;
        if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;     
        o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
        o2.inScaled = false;
        o2.inDither = true;
        fis = new FileInputStream(oldFile);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();

        //scale resized bitmap (version 1 - scaling using createScaledBitmap) 
        int scaledHeight = (int) (640 / ((float)b.getWidth() / (float)b.getHeight()));
        Bitmap bScaled = Bitmap.createScaledBitmap(b, 640, scaledHeight, true);
        b.recycle();

        /*
        //scale resized bitmap (version 2 - scaling using matrix)            
        Matrix matrix = new Matrix();
        float desiredScale = 640 / (float)b.getWidth();
        matrix.postScale(desiredScale, desiredScale);
        Bitmap bScaled = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
        //b.recycle();
        */

        //add timestamp watermark to photo
        String watermark = photoName.substring(0, photoName.length() - 4);
        Bitmap dest = Bitmap.createBitmap(bScaled.getWidth(), bScaled.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas cs = new Canvas(dest);
        Paint tPaint = new Paint();
        tPaint.setTextSize(15);
        tPaint.setColor(Color.RED);
        tPaint.setStyle(Style.FILL);
        tPaint.setAntiAlias(true);
        Paint bScaledPaint = new Paint();
        bScaledPaint.setDither(true);
        cs.drawBitmap(bScaled, 0f, 0f, bScaledPaint);
        float height = tPaint.measureText("yY");
        cs.drawText(watermark, 5f, height+5f, tPaint);
        bScaled.recycle();

        //encode scaled bitmap to jpeg
        FileOutputStream out = new FileOutputStream(newFile);
        dest.compress(Bitmap.CompressFormat.JPEG, 100, out);

        //cleanup
        dest.recycle();
        out.close();

        return "Success";

    } catch (IOException e) {
        ErrorReporter.getInstance().handleException(e);
        return "Error: " + e.toString();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值