2014-03-25 186 views
0

我有一個掃描二維碼的應用程序。該應用程序返回一個名爲value的字符串中的網址。我想在webview中打開網址。我如何在CameraTestActivity中調用WebViewActivity,WebViewActivity使用CameraTestActvity的url來加載網站。代碼中有評論我想做的事情,但我不知道如何。你能幫我麼?我可能因爲這個問題而無法畢業?我已經這樣做了好幾天了。與webview的Qr代碼掃描器

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

相關問題http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in- Android – Gaskoin

+0

@Gaskoin我應該把意圖放在哪裏?在cameraview或webview? – duderbear

回答

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); 
+0

謝謝!我現在知道出了什麼問題。 :) – duderbear

+0

很高興它幫助你,請接受答案,如果它幫助你 –