【android】开发干货-图片合成之生成《飞行员驾驶证》
程序员文章站
2024-03-24 22:57:40
...
现在网上有很多假图片,这些东西都是怎么做出来的呢?
接下来我们用android写一个APP来实现图片合成
注意:请勿用于其它非法用途,发生任何违法事件本人概不负责
主要代码:
1.初始化文字画笔
Typeface typeFace =Typeface.createFromAsset(mContext.getResources().getAssets(),"fonts/Garamond-Bold.ttf");
mPaint = new Paint( Paint.ANTI_ALIAS_FLAG);
mPaint.setTextSize(16);
mPaint.setColor(0xff0e2130);
mPaint.setTypeface(typeFace);
2.画图
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
//创建一个bitmap
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);// 创建一个新的和SRC长度宽度一样的位图
//将该图片作为画布
Canvas canvas = new Canvas(bitmap);
//在画布 0,0坐标上开始绘制原始图片
canvas.drawBitmap(mBitmap, 0, 0, null);
//画姓名
drawText(canvas,name,165,228,2.2f);
//画国籍
drawText(canvas,nationality,295,234,2.2f);
//画驾驶员类型
drawText(canvas,type,205,267,2.2f);
// 保存
canvas.save(Canvas.ALL_SAVE_FLAG);
// 存储
canvas.restore();
3.画文字,并旋转一定角度
void drawText(Canvas canvas ,String text , float x ,float y ,float angle){
if(angle != 0){
canvas.rotate(angle, x, y);
}
canvas.drawText(text, x, y, mPaint);
if(angle != 0){
canvas.rotate(-angle, x, y);
}
}
4.图片分享
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(intent, "Share"));
界面1:
界面2:
Github地址(附Demo下载):
https://github.com/ccwant/PilotCertificate
上一篇: ENVI%2线性拉伸算法实现
下一篇: 仿微信和QQ多图合并框架实现