Android 电子签名,手写签名案列实现方法,并上传网页显示(base64)!

最近说项目可能会用到一个电子签名,不需要识别的那种,只是一个单纯手写签名,然后以base64的格式提供给前端web页面。其实挺简单的,自定义一个手写view就上线了。Android 电子签名,手写签名案列实现方法!
先上图:
这里写图片描述

按钮说明:第一个按钮是清除手写板,第二个是将手写板的内容生成图片并压缩,第三个按钮是触发JS方法,在web页面中显。
布局说明:中间区域是手写屈,左上角是经过大小和质量压缩后的图片,右边是以base64格式上传web页面还原出来的图片。
代码说明:其实很简单,自定义CanvasView继承View
第一步:构造方法里面初始化画布背景、画笔、和路径

 public CanvasView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(Color.WHITE);
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, STROKE_WIDTH, getResources().getDisplayMetrics()));
        paint.setStyle(Paint.Style.STROKE);//设置画笔空心
        paint.setAntiAlias(true);//消除锯齿
        path = new Path();
    }

第二部:画笔归为

   @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(path, paint);
    }

第三步:处理手势,触发画笔

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(x, y);
                break;
        }
        invalidate();
        return true;
    }

到这里,手写功能就已经能使用了,接下来是做一些处理
1、将view生成图片

    //将view生生图片
    public Bitmap createBitmap (View v) {

        int w = v.getWidth();    
        int h = v.getHeight();
        //生成图片
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);    
        Canvas c = new Canvas(bmp);     
        c.drawColor(Color.WHITE); 
        v.layout(0, 0, w, h);    
        v.draw(c);      
        return bmp;

    }

2、图片是否要旋转

    /**
     * 图片旋转
     * @param tmpBitmap
     * @param degrees
     * @return
     */
    public static Bitmap rotateToDegrees(Bitmap tmpBitmap, float degrees) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.setRotate(degrees);
        Bitmap rBitmap = Bitmap.createBitmap(tmpBitmap, 0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight(), matrix,
                true);
        return rBitmap;
    }

3、图片按比例压缩

    /** 
     * 图片按比例大小压缩方法 
     * 
     * @param image (根据Bitmap图片压缩) 
     * @return 
     */  
    public static Bitmap compressScale(Bitmap image) {  

        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  

        // 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出  
        if (baos.toByteArray().length / 1024 > 1024) {  
            baos.reset();// 重置baos即清空baos  
            image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 这里压缩50%,把压缩后的数据存放到baos中  
        }  
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
        BitmapFactory.Options newOpts = new BitmapFactory.Options();  
        // 开始读入图片,此时把options.inJustDecodeBounds 设回true了  
        newOpts.inJustDecodeBounds = true;  
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  
        newOpts.inJustDecodeBounds = false;  
        int w = newOpts.outWidth;//原始宽高 
        int h = newOpts.outHeight;

        // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可  (可根据原始高度计算)
        int be = 4;// be=1表示不缩放 ,缩放比为1/be ,这里缩小为原来的四分之一
        newOpts.inSampleSize = be; // 设置缩放比例  
        // newOpts.inPreferredConfig = Config.RGB_565;//降低图片从ARGB888到RGB565  

        // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了  
        isBm = new ByteArrayInputStream(baos.toByteArray());  
        bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  

        return compressImage(bitmap,5);// 压缩好比例大小后再进行质量压缩  

//        return bitmap;  
    } 

4、图片按照质量压缩

    /** 
     * 质量压缩方法 
     * 
     * @param image  size(kb)
     * @return 
     */  
    public static Bitmap compressImage(Bitmap image,int size) {  

        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中  
        int options = 80;  

        while (baos.toByteArray().length / 1024 > size) { // 循环判断如果压缩后图片是否大于size,大于继续压缩  

            if(options<10) {
                options = 10;
            }

            baos.reset(); // 重置baos即清空baos  
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中  
            options -= 10;// 每次都减少10
        }  
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中  
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片  (PS,这一步操作后,图片质量会变大,没有搞懂为什么,知道的大神可以给我解释下549908016@qq.com)
        return bitmap;  
    }

5、将图片处理成base64的字符串

 /**
    *   
    * 图片转化成base64字符串
    * 创建人:lxj  
    * 创建时间:2018年3月6日 上午10:14:02   
    * @version    
    *
    */
    public  String imageToBase64() {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理  
        String imgFile = Environment.getExternalStorageDirectory()+ "/" + imageName + ".jpg";//待处理的图片  
        InputStream in = null;  
        byte[] data = null;  
        //读取图片字节数组  
        try   
        {  
            in = new FileInputStream(imgFile);          
            data = new byte[in.available()];  
            in.read(data);  
            in.close();  
        }   
        catch (IOException e)   
        {  
            e.printStackTrace();  
        }  
        return Base64.encodeToString(data, Base64.DEFAULT); //返回Base64编码过的字节数组字符串  
    }  
    /** 
     * 将Bitmap转换成Base64字符串 
     * @param bit 
     * @return 
     */  
    public String Bitmap2StrByBase64(Bitmap bit){  
       ByteArrayOutputStream bos=new ByteArrayOutputStream();  
       bit.compress(CompressFormat.JPEG, 100, bos);//参数100表示不压缩  
       byte[] bytes=bos.toByteArray();  
       return Base64.encodeToString(bytes, Base64.DEFAULT);  
    } 

demo下载地址,希望对大家有所帮助:http://download.csdn.net/download/u010886975/10271229

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中实现手写签名的一种方法是使用Canvas和Path类来捕捉手指的移动轨迹,并将其绘制在画布上。下面是一个简单的步骤示例: 1. 创建一个新的Android项目,并在布局文件中添加一个自定义View显示手写签名的画布。 2. 在自定义View的类中,重写onTouchEvent方法,并在其中处理手指的触摸事件。 3. 在onTouchEvent方法中,通过MotionEvent对象获取手指的位置,并根据手指的移动轨迹更新Path对象。 4. 在自定义View的类中,重写onDraw方法,并在其中使用Canvas对象将Path对象绘制在画布上。 5. 在MainActivity中,将自定义View添加到布局文件中,并设置相应的布局参数。 以下是一个简单的示例代码: ```java public class SignatureView extends View { private Path path; private Paint paint; public SignatureView(Context context) { super(context); init(); } public SignatureView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } private void init() { path = new Path(); paint = new Paint(); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawPath(path, paint); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: path.moveTo(x, y); return true; case MotionEvent.ACTION_MOVE: path.lineTo(x, y); break; case MotionEvent.ACTION_UP: // Do something when the finger is lifted break; } invalidate(); return true; } } ``` 在布局文件中添加如下代码: ```xml <com.example.myapplication.SignatureView android:id="@+id/signatureView" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` 在MainActivity中找到SignatureView并设置布局参数: ```java SignatureView signatureView = findViewById(R.id.signatureView); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); signatureView.setLayoutParams(params); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值