我有一个扫描qr代码的应用。该应用程序返回名为value的字符串中的url。我想在webview中打开url。如何在CameraTestActivity中调用WebViewActivity,WebViewActivity使用CameraTestActvity中的URL来加载网站。代码中有关于我想要做的事情的评论,但我不知道如何。你能帮我么?我可能因为这个问题而无法毕业?我已经好几天了。
CameraTestActivity.java
import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.os.Handler;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
/* Import ZBar Class files */
@SuppressLint("SetJavaScriptEnabled")
public class CameraTestActivity extends Activity
{
private Camera mCamera;
private CameraPreview mPreview;
private Handler autoFocusHandler;
TextView scanText;
Button scanButton;
Button exitButton;
WebView web;
ImageScanner scanner;
private boolean barcodeScanned = false;
private boolean previewing = true;
static {
System.loadLibrary("iconv");
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
/* Instance barcode scanner */
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.addView(mPreview);
scanText = (TextView)findViewById(R.id.scanText);
scanButton = (Button)findViewById(R.id.ScanButton);
scanButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (barcodeScanned) {
barcodeScanned = false;
scanText.setText("Place code inside the square to scan");
mCamera.setPreviewCallback(previewCb);
mCamera.startPreview();
previewing = true;
mCamera.autoFocus(autoFocusCB);
}
}
});
exitButton = (Button)findViewById(R.id.ExitButton);
exitButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
}
public void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview); // you need this line
releaseCamera();
}
}
@Override
public void onResume() {
super.onResume();
// Get the Camera instance as the activity achieves full user focus
try {
mCamera = Camera.open();
mCamera.setPreviewCallback(null);
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.addView(mPreview);
mCamera.startPreview();
} catch (Exception e) {
}
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
} catch (Exception e){
}
return c;
}
private Runnable doAutoFocus = new Runnable() {
public void run() {
if (previewing)
mCamera.autoFocus(autoFocusCB);
}
};
PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
String value = new String(sym.getData()); //url scanned from the qr code
if(value.startsWith("http://192.168.1.6")){ //if the url starts with
scanText.setText("QR Code result: " + value);
barcodeScanned = true;
//intent for WebViewActivty.java
//The WebViewActivty.java should load the string value which contains the the url
}
else{
scanText.setText("QR Code result 2: " + value);
barcodeScanned = true;
}
TextView tv = (TextView) findViewById(R.id.scanText);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
};
// Mimic continuous auto-focusing
AutoFocusCallback autoFocusCB = new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}
WebViewActivity.java
package net.sourceforge.zbar.android.CameraTest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.web_engine);
webView.setWebViewClient(new MyWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://192.168.1.6/sp/products.php");//the link should be from the CameraTestActivity
}
}
class MyWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return false;
}
}
答案 0 :(得分:1)
用此替换预览回调。
PreviewCallback previewCb = new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
String value = new String(sym.getData()); //url scanned from the qr code
Intent i = new Intent(CameraTestActivity.this,WebViewActivity.class);
i.putExtra("mUrl",value);
startActivity(i)
}
else{
scanText.setText("QR Code result 2: " + value);
barcodeScanned = true;
}
TextView tv = (TextView) findViewById(R.id.scanText);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
};
并将您的webActivity更改为:
String mUrl = getIntent().getStringExtra("mUrl");
webView = (WebView) findViewById(R.id.web_engine);
webView.setWebViewClient(new MyWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(mUrl);
答案 1 :(得分:0)
如果您使用('me.dm7.barcodescanner:zxing:1.9')库。因此,请遵循此步骤。希望它也对您有用。
Intent newWindow = new Intent(getApplicationContext(), Browser.class);
newWindow.putExtra("scannedLink", myResult);
startActivity(newWindow);
快乐编码