如何使用QR码中的URL重定向到网页?

时间:2016-04-22 06:38:55

标签: android

我正在创建一个qr代码扫描应用程序。我用过ZXing库。 如何使用QR码中的URL重定向到网页? 我想使用我的应用程序重定向到网页(而不是使用浏览器)。请帮我 这是我的代码。

ScanActivity.java

package app.num.barcodescannerproject;

import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;

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

}

public void QrScanner(View view){


    mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
    setContentView(mScannerView);

    mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
    mScannerView.startCamera();         // Start camera

}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();           // Stop camera on pause
}

@Override
public void handleResult(Result rawResult) {
    // Do something with the result here

    Log.e("handler", rawResult.getText()); 
    Log.e("handler", rawResult.getBarcodeFormat().toString()); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Kết quả :");
    builder.setMessage(rawResult.getText());
    AlertDialog alert1 = builder.create();
    alert1.show();


  mScannerView.resumeCameraPreview(this);
}
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.num.barcodescannerproject.MainActivity">

<Button android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="QRScanner"
    android:onClick="QrScanner" />
</RelativeLayout>

ActivityMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.num.barcodescannerproject">
<uses-permission android:name="android.permission.CAMERA" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我假设QR读取成功。它的输出是

rawResult.getText() // working url

获取url后,您需要将URL传递/加载到WebView。

对于您需要提供的WebView,

<uses-permission android:name="android.permission.INTERNET" />

到您的清单文件。

更多细节参考,

[WebView]:http://developer.android.com/reference/android/webkit/WebView.html