Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的数字签名_Android - Fatal编程技术网

android中的数字签名

android中的数字签名,android,Android,我想在android中创建数字签名应用程序。它应该捕获用户签名并将其存储为图像。如果有人知道,请告诉我。这可以通过手势叠加来完成。这在APIDemos中得到了演示。以下链接必须有帮助: 尝试自定义视图而不是手势: package com.example.myapp.gui.views; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import an

我想在android中创建数字签名应用程序。它应该捕获用户签名并将其存储为图像。如果有人知道,请告诉我。

这可以通过手势叠加来完成。这在APIDemos中得到了演示。以下链接必须有帮助:


尝试自定义视图而不是手势:

package com.example.myapp.gui.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * A simple view to capture a path traced onto the screen. Initially intended to be used to captures signatures.
 * 
 * @author Andrew Crichton
 * @version 0.1
 */
public class SignatureView extends View {
    private Path mPath;
    private Paint mPaint; 
    private Paint bgPaint = new Paint(Color.TRANSPARENT);

    private Bitmap mBitmap;
    private Canvas mCanvas;

    private float curX, curY;

    private static final int TOUCH_TOLERANCE = 4;
    private static final int STROKE_WIDTH = 4;

    public SignatureView(Context context) {
        super(context);
        init();
    }
    public SignatureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public SignatureView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    private void init() {
        setFocusable(true);
        mPath = new Path();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(STROKE_WIDTH);
    }
    public void setSigColor(int color) {
        mPaint.setColor(color);
    }
    public void setSigColor(int a, int red, int green, int blue) {
        mPaint.setARGB(a, red, green, blue);
    }
    public boolean clearSignature() {
        if (mBitmap != null)
            createFakeMotionEvents();
        if (mCanvas != null) {
            mCanvas.drawColor(Color.BLACK);
            mCanvas.drawPaint(bgPaint);
            mPath.reset();
            invalidate();
        }
        else {
            return false;
        }
        return true;
    }
    public Bitmap getImage() {
        return this.mBitmap;
    }
    public void setImage(Bitmap bitmap) {
        this.mBitmap = bitmap;
        this.invalidate();
    }
    @Override
    protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
        int bitmapWidth = mBitmap != null ? mBitmap.getWidth() : 0;
        int bitmapHeight = mBitmap != null ? mBitmap.getWidth() : 0;
        if (bitmapWidth >= width && bitmapHeight >= height) 
            return;
        if (bitmapWidth < width) 
            bitmapWidth = width;
        if (bitmapHeight < height) 
            bitmapHeight = height;
        Bitmap newBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
        Canvas newCanvas = new Canvas();
        newCanvas.setBitmap(newBitmap);
        if (mBitmap != null) 
            newCanvas.drawBitmap(mBitmap, 0, 0, null);
        mBitmap = newBitmap;
        mCanvas = newCanvas;
    }
    private void createFakeMotionEvents() {
        MotionEvent downEvent = MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis()+100, MotionEvent.ACTION_DOWN, 1f, 1f ,0);
        MotionEvent upEvent = MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis()+100, MotionEvent.ACTION_UP, 1f, 1f ,0);
        onTouchEvent(downEvent);
        onTouchEvent(upEvent);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(mBitmap, 0, 0, mPaint); 
        canvas.drawPath(mPath, mPaint);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touchDown(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            touchMove(x, y);
            break;
        case MotionEvent.ACTION_UP:
            touchUp();
            break;
        }
        invalidate();
        return true;
    }
    /**----------------------------------------------------------
     * Private methods
     **---------------------------------------------------------*/

    private void touchDown(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        curX = x;
        curY = y;
    }

    private void touchMove(float x, float y) {
        float dx = Math.abs(x - curX);
        float dy = Math.abs(y - curY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(curX, curY, (x + curX)/2, (y + curY)/2);
            curX = x;
            curY = y;
        }
    }

    private void touchUp() {
        mPath.lineTo(curX, curY);
        if (mCanvas == null) {
            mCanvas = new Canvas();
            mCanvas.setBitmap(mBitmap);
        }
        mCanvas.drawPath(mPath, mPaint);
        mPath.reset();
    }
}
将sd卡根目录上的签名保存为signature.jpeg。对于编写部分,请确保在清单中具有该权限:

希望此代码对您有所帮助:)

esign_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <android.gesture.GestureOverlayView
        android:id="@+id/signaturePad"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:background="@android:color/white"
        android:clickable="false"
        android:eventsInterceptionEnabled="true"
        android:fadeEnabled="false"
        android:gestureColor="#0000ff"
        android:gestureStrokeLengthThreshold="0.1"
        android:gestureStrokeType="multiple"
        android:longClickable="false"
        android:orientation="vertical"
        android:uncertainGestureColor="#000000"
        android:splitMotionEvents="true" 
        android:fadeOffset="10000000">

    </android.gesture.GestureOverlayView>

    <RelativeLayout
        android:id="@+id/rellay_esign_donebutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
         >
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >
        <Button
            android:id="@+id/DoneButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Done" />

         <Button
             android:id="@+id/ClearButton"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Clear" />
</LinearLayout>
    </RelativeLayout>

</LinearLayout>

对于白色背景的jpg文件:

gestureView.setDrawingCacheBackgroundColor(Color.WHITE);

使用解决方案。

多亏了,这里有一种不用访问文件系统就可以获取位图的方法

是的,
gestureView.buildDrawingCache()
API已被弃用,但就我而言,目前这是一个更可接受的解决方案。。而不是要求用户获得保存权限,以便仅为这一项功能支持整个应用程序

    // .. continuing from Harhal's code here:

    donebutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try {
                gestureView.buildDrawingCache();
                bitmap = gestureView.getDrawingCache();

                // set bitmap somewhere
                // eg: mBinding.signature.setImageBitmap(bitmap);
            }
            catch (Exception e) {
                e.printStackTrace();
            }

            // Activity Stuff ...
        }
    });
要重置视图,
gestureView
在加载的第一个位图上为我卡住了。因此,重新加载同一个片段似乎是让所有内容干净地重置的最简单方法

下面是这方面的伪代码,因为每个人似乎都以不同的方式管理他们的片段堆栈


我添加了一些代码,现在它将覆盖整个问题。当它工作时,检查作为答案…非常感谢您的友好!!但当我画线或任何形状时,为什么它会移动它的位置?请你帮个忙好吗?@Rauf你是说当你画画的时候,安卓会在你画画的地方画上几(几十)个像素吗?不。我画了一条从a点(200200)到B点(200400)的线,然后当我松开指针/触摸线就会移到左上角,比如X(50,50)到Z(50250)…我尽力解释:)我有个问题。当我将背景颜色更改为白色(在代码中)时,它会起作用。但是,只要我调用clear()函数,它就会再次变黑:/代码的源代码可以在这里找到
gestureView.setDrawingCacheBackgroundColor(Color.WHITE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    // .. continuing from Harhal's code here:

    donebutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try {
                gestureView.buildDrawingCache();
                bitmap = gestureView.getDrawingCache();

                // set bitmap somewhere
                // eg: mBinding.signature.setImageBitmap(bitmap);
            }
            catch (Exception e) {
                e.printStackTrace();
            }

            // Activity Stuff ...
        }
    });
    clearButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // Reload this fragment
            // 1. Pop current fragment (current instance) from fragment stack
            // 2. Set this same fragment again 

        }
    });