Android图形验证码工具类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lou_liang/article/details/80353263

移动端获取图形验证码有两种形式,一种是在本地随机生成进行验证,另一种是根据服务端返回的图片流设置到页面上,今天我们要做的就是根据服务端返回的操作图形验证码,完整demo地址:https://download.csdn.net/download/lou_liang/10421144          下面看一张图:


demo中是一个底部弹出的dialog工具类,结合服务端返回的图片流显示出来。

首先自定义Dialog工具类,代码中包含点击确定按钮的接口回调部分,主要是为了根据服务器返回的图片验证码,在输入框中输入验证码,接着将输入的数字再传给服务器,以此判断是否输入验证码正确

public class PictureDialog {
    public interface  onListener{
        void OnListener(String msg);
    }
    private static onListener listeners;
    public void setListener(onListener listener){
        this.listeners = listener;
    }
    public static class Builder {
        private Context mContext;
        private Dialog mDialog;
        private ViewHolder mViewHolder;
        private View mView;
        public Builder(Context context) {
            mContext = context;
            initView();
        }
        public Builder setContent(String img){
            Glide.with(mContext).load(img).asBitmap().into(mViewHolder.ivPic);
            return this;
        }
        public Builder setPositiveButton(CharSequence sure, final View.OnClickListener listener) {
            mViewHolder.tvPositiveButton.setText(sure);

            mViewHolder.tvPositiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(TextUtils.isEmpty(mViewHolder.normalMessage.getText().toString())){
                        Toast.makeText(mContext,"验证码不能为空",Toast.LENGTH_LONG).show();
                    }else {
                        mDialog.dismiss();
                        if (listener != null) {
                            listener.onClick(view);
                        }
                        if(listeners != null){
                            listeners.OnListener(mViewHolder.normalMessage.getText().toString());
                        }
                    }
                }
            });
            return this;
        }
        public Builder setNegativeButton(CharSequence text, final View.OnClickListener listener) {
            mViewHolder.tvNegativeButton.setText(text);
            mViewHolder.tvNegativeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    if (listener != null) {
                        listener.onClick(view);
                    }
                }
            });
            return this;
        }
        
        public void show() {
            if (mDialog != null) {
                mDialog.show();
            }
        }
        public void dismiss() {
            if (mDialog != null) {
                mDialog.dismiss();
            }
        }
        private void initView() {
            mDialog = new Dialog(mContext,DialogUtils.getStyle());
            mView = LayoutInflater.from(mContext).inflate(R.layout.normal_dialog, null);
            mViewHolder = new ViewHolder(mView);
            mDialog.setContentView(mView);
            mDialog.getWindow().setGravity(Gravity.BOTTOM);
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
            lp.width = (int) (dm.widthPixels * 335.0 / 360.0);
            mDialog.getWindow().setAttributes(lp);
        }
        class ViewHolder {
            ImageView ivPic;
            EditText normalMessage;
            TextView tvNegativeButton;
            TextView tvPositiveButton;
            public ViewHolder(View view) {

                ivPic = (ImageView) view.findViewById(R.id.iv_pic);                   //图形验证码
                normalMessage = (EditText) view.findViewById(R.id.dialog_normal_message);//输入内容
                tvNegativeButton = (TextView) view.findViewById(R.id.dialog_negative);  //取消
                tvPositiveButton = (TextView) view.findViewById(R.id.dialog_positive);   //确定
            }
        }
    }
}

最后在MainActivity中点击加载按钮调用dialog:其中要注意filePath代表的图片流要根据大家自己服务器返回的来取,我这里是内网,其他人获取不到。

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final PictureDialog.Builder builder1 = new PictureDialog.Builder(MainActivity.this);
        //二进制图片流根据服务端返回的来取
        String filePath ="http://192.168.6.51:36030/m/passport/register/getCaptcha.jpg?smsStoken=captcha_stoken_9e56b995c197458e9d6452851de01631";
        builder1.setContent(filePath);
        builder1.setPositiveButton( "确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PictureDialog pictureDialog = new PictureDialog();
                pictureDialog.setListener(new PictureDialog.onListener() {
                    @Override
                    public void OnListener( String msg) {
                        //此text为输入的验证码,提交到服务器判断输入的验证码是否正确
                        String text = msg;
                        builder1.dismiss();
                    }
                });
            }
        }).setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder1.dismiss();
            }
        }).show();
    }
});
到这里,图形验证码dialog工具类就写好了,希望对大家有帮助,谢谢。附上源码demo:https://download.csdn.net/download/lou_liang/10421144
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页