当前位置:   article > 正文

在app端手写签名并保存下来(以文件形式或者是base64编码)_手寫簽名功能db用什麼字段保存

手寫簽名功能db用什麼字段保存

最近做的项目有个需求,在app端手写签名的并保存为文件或者base64编码。所以就刚好有时间写了一下并记录下来:

先上效果图:

 

分析思路:因为这里是要获取手势轨迹并画出来,所以肯定是有个Touch事件,然后使用要回显,那肯定是要获取所画的内容,所以想到了canvas这个东西,那这样就选择了自定义控件来做了。

  1. import android.content.Context;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Bitmap.Config;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Path;
  8. import android.graphics.PorterDuff;
  9. import android.util.AttributeSet;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. /**
  13. * 文件描述:
  14. * 作者:徐干稳
  15. * 创建时间:2019/11/21
  16. * 更改时间:2019/11/21
  17. * 版本号:1.0
  18. */
  19. public class HandWriteView extends View {
  20. private Paint paint;
  21. private Path path;
  22. public Bitmap cachebBitmap;
  23. private Canvas cacheCanvas;
  24. private boolean clearFlag = false;
  25. public HandWriteView(Context context) {
  26. this(context, null);
  27. }
  28. public HandWriteView(Context context, AttributeSet attrs) {
  29. super(context, attrs);
  30. // TODO Auto-generated constructor stub
  31. paint = new Paint();
  32. paint.setAntiAlias(true);
  33. paint.setStrokeWidth(3);
  34. paint.setStyle(Paint.Style.STROKE);
  35. paint.setColor(Color.BLACK);
  36. path = new Path();
  37. /*cachebBitmap = Bitmap.createBitmap(1080, 1800, Config.ARGB_8888);
  38. cacheCanvas = new Canvas(cachebBitmap);
  39. cacheCanvas.drawColor(Color.WHITE);*/
  40. }
  41. @Override
  42. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  43. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  44. cachebBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Config.ARGB_8888);
  45. cacheCanvas = new Canvas(cachebBitmap);
  46. cacheCanvas.drawColor(Color.WHITE);
  47. }
  48. @Override
  49. protected void onDraw(Canvas canvas) {
  50. Paint bmpPaint = new Paint();
  51. canvas.drawBitmap(cachebBitmap, 0, 0, bmpPaint);
  52. canvas.drawPath(path, paint);
  53. }
  54. float cur_x;
  55. float cur_y;
  56. @Override
  57. public boolean onTouchEvent(MotionEvent event) {
  58. float x = event.getX();
  59. float y = event.getY();
  60. switch (event.getAction()) {
  61. case MotionEvent.ACTION_DOWN:
  62. cur_x = x;
  63. cur_y = y;
  64. path.moveTo(cur_x, cur_y);
  65. break;
  66. case MotionEvent.ACTION_MOVE:
  67. path.quadTo(cur_x, cur_y, x, y);
  68. cur_x = x;
  69. cur_y = y;
  70. break;
  71. case MotionEvent.ACTION_UP:
  72. cacheCanvas.drawPath(path, paint);
  73. path.reset();
  74. break;
  75. default:
  76. break;
  77. }
  78. invalidate();
  79. return true;
  80. }
  81. /**
  82. * 清除画布内容
  83. */
  84. public void clearCanvas() {
  85. cacheCanvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_OVER);
  86. invalidate();
  87. }
  88. }

主界面代码:

  1. package com.xuganwen.testandroidpie;
  2. import android.content.pm.ActivityInfo;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.os.Environment;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Base64;
  9. import android.view.View;
  10. import android.widget.ImageView;
  11. import android.widget.Toast;
  12. import java.io.ByteArrayInputStream;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. public class MainActivity extends AppCompatActivity {
  21. private MyPieView pieView;
  22. private List<DataBean> mDatas = new ArrayList<>();
  23. private HandWriteView handWriteView;
  24. private ImageView imageview;
  25. private String base64;
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  30. setContentView(R.layout.activity_main);
  31. /* dashview=findViewById(R.id.dashview);
  32. ArrayList<String> strings = new ArrayList<>();
  33. strings.add("10.0");
  34. strings.add("7.5");
  35. strings.add("5.0");
  36. strings.add("2.5");
  37. dashview.setArrowRotateDrgee(135);
  38. dashview.setValues(strings);
  39. dashview.setCurrentValue(2.2f);
  40. dashview.invalidate();*/
  41. handWriteView = findViewById(R.id.handwrite);
  42. findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View v) {
  45. handWriteView.clearCanvas();
  46. imageview.setVisibility(View.GONE);
  47. }
  48. });
  49. findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
  50. @Override
  51. public void onClick(View v) {
  52. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  53. handWriteView.cachebBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
  54. byte[] buffer = outputStream.toByteArray();
  55. base64 = byteToBase64(buffer);
  56. // Toast.makeText(getApplicationContext(),base64+"",Toast.LENGTH_SHORT).show();
  57. }
  58. });
  59. findViewById(R.id.btn_show).setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. if (null != base64) {
  63. imageview.setVisibility(View.VISIBLE);
  64. byte[] decode = Base64.decode(base64.split(",")[1], Base64.DEFAULT);
  65. Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
  66. imageview.setImageBitmap(bitmap);
  67. }
  68. }
  69. });
  70. imageview = findViewById(R.id.imageview);
  71. }
  72. public static String byteToBase64(byte[] bytes) {
  73. String base64 = null;
  74. InputStream in = null;
  75. try {
  76. in = new ByteArrayInputStream(bytes);
  77. base64 = "data:image/png;base64," + android.util.Base64.encodeToString(bytes, Base64.DEFAULT);
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. } finally {
  81. if (in != null) {
  82. try {
  83. in.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. return base64;
  90. }
  91. /**
  92. * base64显示为图片
  93. */
  94. public void showBase64ToImage(ImageView imageView, String base64) {
  95. if (null != imageView && null != base64) {
  96. byte[] decode = Base64.decode(base64.split(",")[1], Base64.DEFAULT);
  97. Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
  98. imageView.setImageBitmap(bitmap);
  99. }
  100. }
  101. /**
  102. * cachebitmap转文件存储下来
  103. */
  104. public void saveToFile() {
  105. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  106. handWriteView.cachebBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
  107. byte[] buffer = outputStream.toByteArray();
  108. if (buffer != null) {
  109. try {
  110. File file = new File(Environment.getExternalStorageDirectory(), "sign.jpg");
  111. file.createNewFile();
  112. new FileOutputStream(file).write(buffer);
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. }
  116. }
  117. Toast.makeText(this, "生成图片文件", Toast.LENGTH_SHORT).show();
  118. }
  119. }

布局代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <!--<com.xuganwen.testandroidpie.MyPieView
  10. android:layout_width="200dp"
  11. android:layout_height="120dp"
  12. android:id="@+id/pieView"
  13. android:visibility="gone"
  14. android:layout_marginTop="10dp"/>
  15. <com.xuganwen.testandroidpie.DashBoardVIew
  16. android:layout_width="120dp"
  17. android:id="@+id/dashview"
  18. android:layout_height="120dp" />-->
  19. <Button
  20. android:layout_width="match_parent"
  21. android:layout_height="40dp"
  22. android:id="@+id/btn_clear"
  23. android:text="清除"
  24. />
  25. <Button
  26. android:layout_width="match_parent"
  27. android:layout_height="40dp"
  28. android:id="@+id/btn_save"
  29. android:text="保存"
  30. />
  31. <Button
  32. android:layout_width="match_parent"
  33. android:layout_height="40dp"
  34. android:id="@+id/btn_show"
  35. android:text="回显"
  36. />
  37. <RelativeLayout
  38. android:layout_width="match_parent"
  39. android:layout_height="match_parent">
  40. <com.xuganwen.testandroidpie.HandWriteView
  41. android:layout_width="match_parent"
  42. android:layout_height="match_parent"
  43. android:id="@+id/handwrite"
  44. />
  45. <ImageView
  46. android:layout_width="match_parent"
  47. android:layout_height="match_parent"
  48. android:visibility="gone"
  49. android:id="@+id/imageview"/>
  50. </RelativeLayout>
  51. </LinearLayout>

以上就是图片中显示效果的所有代码,几年前写的东西没想到今天会用到项目中,所以在这里记录下来,说不定以后还能用到。。。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/261938
推荐阅读
  

闽ICP备14008679号