二维码扫描与生成二维码

这个网址讲的很详细,就是我做时有个问题
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.baway.sixday/com.xys.libzxing.zxing.activity.CaptureActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

您需要使用Theme.AppCompat主题(或后代) 与此活动。

我们扫描就是要用到这个CaptureActivity类,直接把上面下载地址里面下载了里面的libzxing作为Module,如下图:

二维码扫描与生成二维码

二维码扫描与生成二维码



导入权限

<!-- 相机 -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 振动 -->
<uses-permission android:name="android.permission.VIBRATE" />

导入activity我们既然把它作为Module了,那么我们也是可以拿来直接用,这里我们可以直接把依赖里面的关于CaptureActivity类的AndroidManifest.xml的注册信息拷贝过来放在我们这个项目中:

这个注释的就是原先activity中的,但是使用它的话就会出现上面的错误,所以下面已经进行了更改

所以在改变后还是无法解决问题,可以看看这个网站的解决方式 http://www.xuebuyuan.com/2233181.html

<!--android:theme="@android:style/Theme.NoTitleBar.Fullscreen"-->
<activity
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    android:name="com.xys.libzxing.zxing.activity.CaptureActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="portrait"

    android:windowSoftInputMode="stateAlwaysHidden">

</activity>

然后在values文件夹下的styles中加入新的主题资源

<!--添加的主题资源-->
<style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
</style>
---------------
二维码扫描与生成二维码


然后就是布局

<?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="text.bwie.com.erweima2.MainActivity">

    <Button
        android:id="@+id/btnSan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫描二维码" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_marginTop="60dp"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_content"
        android:layout_marginTop="20dp"
        android:hint="请输入要生成的二维码文字" />

    <Button
        android:id="@+id/btn_generate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_input"
        android:layout_marginTop="20dp"
        android:text="生成二维码" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_generate"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" />
</LinearLayout>
--------------------------------

最后就是主类中写的内容了

public class MainActivity extends AppCompatActivity  {
    private TextView tv_content;
    private EditText et_input;
    private ImageView img;
    /**
     * 扫描二维码
     */
    private Button mBtnSan;
    private TextView mTvContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_content = (TextView) findViewById(R.id.tv_content);
        et_input = (EditText) findViewById(R.id.et_input);
        img = (ImageView) findViewById(R.id.img);
        findViewById(R.id.btnSan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0);
            }
        });

        findViewById(R.id.btn_generate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = et_input.getText().toString();
                if (str.equals("")) {
                    Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    // 位图
                    try {
                        /**
                         * 参数:1.文本 2 3.二维码的宽高 4.二维码中间的那个logo
                         */
                        Bitmap bitmap = EncodingUtils.createQRCode(str, 500, 500, null);
                        // 设置图片
                        img.setImageBitmap(bitmap);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String result = data.getExtras().getString("result");
            tv_content.setText(result);
        }
    }

}
这就是最后的结果了

二维码扫描与生成二维码