扫一扫

=================================================================主页面
public class MainActivity extends AppCompatActivity {
    private TextView mTxt;
    private ImageView mImage;
    private EditText mEdit;
    private final int RESULT_CODE = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTxt = (TextView) findViewById(R.id.tv_mess);
        mImage = (ImageView) findViewById(R.id.image);
        mEdit = (EditText) findViewById(R.id.edit);
        findViewById(R.id.sao).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sCan();
            }
        });
        findViewById(R.id.dai).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setCaptrue(0);
            }
        });
        //不带logo
        findViewById(R.id.budai).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setCaptrue(1);
            }
        });
    }

    //生成图片
    private void setCaptrue(int type) {
        String message = mEdit.getText().toString().trim();
        if (TextUtils.isEmpty(message)) {
            Toast.makeText(this, "请输入一点东西啊", Toast.LENGTH_SHORT).show();
            return;
        }

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        if (type == 0) {
            bitmap = null;
        }
        try {
            Bitmap bitmap2 = CodeCreator.createQRCode(message, 200, 200, bitmap);
            mImage.setImageBitmap(bitmap2);
        } catch (WriterException e) {
            e.printStackTrace();
        }


    }

    private void sCan() {
        PermissionUtils.permission(this, new PermissionUtils.PermissionListener() {
            @Override
            public void success() {
                Intent intent = new Intent(MainActivity.this, CaptureActivity.class);

//                ZxingConfig config=new ZxingConfig();
//                config.setShowbottomLayout(false);
//                config.setPlayBeep(true);
//                intent.putExtra(Constant.INTENT_ZXING_CONFIG,config);
                startActivityForResult(intent, RESULT_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_CODE && resultCode == RESULT_OK) {
            String result = data.getStringExtra(Constant.CODED_CONTENT);
            if (result.contains("http")) {
                //跳转
                Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
                intent.putExtra("web_url", result);
                startActivity(intent);
            } else {
                mTxt.setText(result);
            }

        }

    }
}

============================================WebViewActivity

package zhoukao1.bwie.com.zxapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web);
        WebView mWebView= (WebView) findViewById(R.id.webview);

        String url=getIntent().getStringExtra("web_url");
        mWebView.setWebViewClient(new WebViewClient());
        mWebView.loadUrl(url);

    }
}

================布局

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:id="@+id/tv_mess"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="扫一扫"
        android:id="@+id/sao"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/edit"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="二维码生成图片带logo"
        android:id="@+id/dai"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="二维码生成图片不带logo"
        android:id="@+id/budai"
        />

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:id="@+id/image"
        />

</LinearLayout>

==================web

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WebViewActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/qq_43131935/article/details/82981577