Android(安卓)图片加水印
16lz
2022-03-04
Android 图片加水印
首先可以肯定 java.awt 在android中是不可用的
直接上代码:
/** * * @param src 原图片 * @param watermark 要打的水印图片 * @param title 要打的水印文字 * @param densityDpi 屏幕位深密度 * @return Bitmap 打好水印的图片 */public static Bitmap createBitmap(String file, Bitmap watermark, String title,int densityDpi) {int dpi = densityDpi;LogPrint.Print("watermark===createBitmap====dpi"+dpi);// 读取原图片信息 File srcImgFile = new File(file); Bitmap roratesrc = BitmapFactory.decodeFile(file);Bitmap src = rorateBitamp(90, roratesrc);if (src == null) {return null;}int srcWidth = src.getWidth();int srcHeight = src.getHeight();Paint paint = new Paint();// ARGB_8888Bitmap newb = Bitmap.createBitmap(srcWidth, srcHeight, Config.ARGB_8888);// 创建一个新的和src长度宽度一样的位图// 把创建的位图作为画板Canvas cv = new Canvas(newb);// 在0,0坐标开始画入srccv.drawBitmap(src, 0, 0, paint);if (watermark != null) {int ww = watermark.getWidth();int wh = watermark.getHeight();paint.setAlpha(50);// cv.drawBitmap(watermark, srcWidth - ww + 1, srcHeight - wh +1,paint);// 在src的右下角画入水印cv.drawBitmap(watermark, 1, 1, paint);// 在src的左上角画入水印}// 加入文字if (title != null) {String familyName = "宋体";Typeface font = Typeface.create(familyName, Typeface.NORMAL);TextPaint textPaint = new TextPaint();textPaint.setColor(Color.WHITE);textPaint.setTypeface(font);if(dpi <= 120){//qvga 240X400textPaint.setTextSize(5);}else if(dpi <= 160){//hvga 320X480textPaint.setTextSize(8);}else if(dpi <= 240){//wvga 480X800textPaint.setTextSize(10);if (Configs.WIDTH > 700) {textPaint.setTextSize(15);}}else if(dpi <= 320){// 1280*720textPaint.setTextSize(15);if (Configs.WIDTH > 2000 ) {textPaint.setTextSize(72);}}else { //更大屏幕分辨率textPaint.setTextSize(72);}// 这里是自动换行的StaticLayout layout = new StaticLayout(title, textPaint, srcWidth,Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);layout.draw(cv);// 文字加在左上角//cv.drawText(title,40,40,paint);} else {paint.setColor(Color.WHITE);paint.setTextSize(20);cv.drawText("测试", 1, 1, paint);}cv.save(Canvas.ALL_SAVE_FLAG);// 保存cv.restore();// 存储ByteArrayOutputStream baos = new ByteArrayOutputStream();newb.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] bytes = baos.toByteArray();try {FileOutputStream fileOutputStream = new FileOutputStream(srcImgFile);fileOutputStream.write(bytes);fileOutputStream.flush();fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}return newb;}
示例:
//图片添加文字水if (mlocation != null) { BitmapUtils.createBitmap(mPictureFile.getAbsolutePath(), null,"经纬度:"+mlocation.getLongitude()+","+mlocation.getLatitude()+"\t\t角度:"+mlocation.getBearing()+"度\t\t"+"方向:"+sensorOrientation+"度\t\t速度:"+mlocation.getSpeed()+"公里/小时\n拍摄时间:"+imageFormat.format(System.currentTimeMillis()),getResources().getDisplayMetrics().densityDpi); }else { BitmapUtils.createBitmap(mPictureFile.getAbsolutePath(), null,"经纬度:"+0+","+0+"\t\t角度:"+0+"度\t\t"+"方向:"+sensorOrientation+"度\t\t速度:"+0+"公里/小时\n拍摄时间:"+imageFormat.format(System.currentTimeMillis()),getResources().getDisplayMetrics().densityDpi); }
更多相关文章
- Android(安卓)照片压缩
- android背景选择器selector用法汇总
- 文字位于RadioButton后,如何调整间距?
- 通过 http post 方式上传多张图片
- Android(安卓)gallery实现图片的左右循环旋转源码分享
- android 选择器selector的用法说明
- Android(安卓)BitmapFactory.Options 介绍
- android zxing 解析二维码图片与生成二维码图片
- Android(安卓)上传图片到服务器(多文件上传)