0

Может кто-нибудь сказать мне, как закрыть экран (который открывается с помощью BarcodeScanner) и показать главный экран снова после вызова метода barcodeDecoded?Blackberry BarcodeScanner - barcodeDecode switch to MainScreen

Я не могу понять это правильно. Я пробовал много, один из них был такой:

public void barcodeDecoded(String rawText) {   
    final String result = rawText; 
    try 
    { 
     final UiApplication ui = UiApplication.getUiApplication(); 
     final MainScreen current = (MainScreen) ui.getActiveScreen(); 
     System.out.println("Current: " + current.toString()); 

     if (UiApplication.isEventDispatchThread()) { 
      getText(result); 
      ui.popScreen(current); 
      System.out.println("Close Window by active screen"); 

      ui.pushScreen(_frm); 
      System.out.println("Push screen frmMain"); 
     }else{ 
      ui.invokeLater(new Runnable() { 
       public void run() { 
        getText(result); <-- Abstract method to use within the main app. 
        ui.popScreen(current); 
        ui.pushScreen(_frm); 
       } 
      }); 
     } 
    }catch(Exception err){ 
     System.out.println(err.getMessage()); 
    } 

} 

абстрактный метод, когда я запустить сканер

private MenuItem mnuCamera = new MenuItem("Scan", 1, 1){ 
    public void run(){ 
     frmMain f = (frmMain)getScreen(); 
     _decode = new BarcodeDecoderClass(f) { 
      public void getText(String tekst) { 
       setScannedText(tekst); 
      } 
     }; 
     _decode.Start(); 
    } 

}; 

ответ

0

Ok, для людей, которые застряли с той же проблемой. Я понял. Ниже вы найдете полный код:

BarcodeScanner класс:

public abstract class BarcodeDecoderClass implements BarcodeDecoderListener { 

private Hashtable _hints; 
private Vector _formats; 
private BarcodeScanner _scanner; 
private BarcodeDecoder _decoder; 
private Field _viewFinder; 
private MainScreen _screen; 
public abstract void getText(String tekst, Screen screen); 

public BarcodeDecoderClass(){ 
    _hints = new Hashtable(); 
    _formats = new Vector(); 
    _formats.addElement(BarcodeFormat.QR_CODE); 
    _hints.put(DecodeHintType.POSSIBLE_FORMATS, _formats); 
    _decoder = new BarcodeDecoder(_hints); 

    try 
    { 
     _scanner = new BarcodeScanner(_decoder, this); 
     _scanner.getVideoControl().setDisplayFullScreen(true); 
     _viewFinder = _scanner.getViewfinder();       

    }catch(Exception err){ 
     System.out.println(err.getMessage()); 
    } 
} 

public void Start(){ 
    try 
    { 
     _screen = new MainScreen(); 
     _screen.add(_viewFinder); 
     UiApplication.getUiApplication().pushScreen(_screen); 
     _scanner.startScan(); 
    }catch(Exception err){ 
     System.out.println(err.getMessage()); 
    } 
} 

public synchronized void Close(){ 
    if(_scanner.isScanning()){ 
     try{ 
      _scanner.stopScan(); 
     }catch(Exception err){ 
      Dialog.alert(err.getMessage()); 
     } 
    } 
    _scanner.getVideoControl().setVisible(false); 
    _scanner.getPlayer().close(); 
} 

public void barcodeDecoded(String rawText) {   
    try 
    { 
     getText(rawText, _screen); 
    }catch(Exception err){ 
     System.out.println(err.getMessage()); 
    } 
} 

}

В начало, из которого я начинаю BarcodeScanner (я просто скопировал метод)

private MenuItem mnuCamera = new MenuItem("Scan", 1, 1){ 
    public void run(){ 
     final Screen f = getScreen(); 
     _decode = new BarcodeDecoderClass() { 
      public void getText(String tekst, final Screen _screen) { 
       setScannedText(tekst); 
       UiApplication.getUiApplication().invokeLater(new Runnable() { 
        public void run() { 
         _decode.Close(); 
         _screen.close(); 
        } 
       }); 
      } 
     }; 
     _decode.Start(); 
    }   
}; 
0

мая помогите заполнить этот код.

import java.util.Hashtable; 
import java.util.Vector; 


import net.rim.device.api.barcodelib.BarcodeDecoder; 

import net.rim.device.api.barcodelib.BarcodeDecoderListener; 
import net.rim.device.api.barcodelib.BarcodeScanner; 
import net.rim.device.api.system.KeyListener; 
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.FieldChangeListener; 
import net.rim.device.api.ui.Keypad; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.container.FullScreen; 
import net.rim.device.api.ui.container.MainScreen; 

import com.google.zxing.BarcodeFormat; 
import com.google.zxing.DecodeHintType; 


public class BarcodeScanSample extends MainScreen{ 
private FullScreen _barcodeScreen; 
private BarcodeScanner _scanner; 
private LabelField lblBarcodeText; 
private ButtonField btnScan; 

public BarcodeScanSample(String barcodeText){ 
    lblBarcodeText = new LabelField(barcodeText); 
    add(lblBarcodeText); 
    btnScan = new ButtonField("Scan"); 
    btnScan.setChangeListener(new FieldChangeListener() { 

     public void fieldChanged(Field field, int context) { 
      scanBarcode(); 
     } 
    }); 
    add(btnScan); 
} 
private void scanBarcode() { 
    // If we haven't scanned before, we will set up our barcode scanner 
    if (_barcodeScreen == null) { 

     // First we create a hashtable to hold all of the hints that we can 
     // give the API about how we want to scan a barcode to improve speed 
     // and accuracy. 
     Hashtable hints = new Hashtable(); 

     // The first thing going in is a list of formats. We could look for 
     // more than one at a time, but it's much slower. and set Barcode Format. 
     Vector formats = new Vector(); 
     formats.addElement(BarcodeFormat.QR_CODE); 
     formats.addElement(BarcodeFormat.CODE_128); 
     formats.addElement(BarcodeFormat.CODE_39); 
     formats.addElement(BarcodeFormat.DATAMATRIX); 
     formats.addElement(BarcodeFormat.EAN_13); 
     formats.addElement(BarcodeFormat.EAN_8); 
     formats.addElement(BarcodeFormat.ITF); 
     formats.addElement(BarcodeFormat.PDF417); 
     formats.addElement(BarcodeFormat.UPC_A); 
     formats.addElement(BarcodeFormat.UPC_E); 

     hints.put(DecodeHintType.POSSIBLE_FORMATS, formats); 

     // We will also use the "TRY_HARDER" flag to make sure we get an 
     // accurate scan 
     hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); 

     // We create a new decoder using those hints 
     BarcodeDecoder decoder = new BarcodeDecoder(hints); 

     // Finally we can create the actual scanner with a decoder and a 
     // listener that will handle the data stored in the barcode. We put 
     // that in our view screen to handle the display. 
     try { 
      _scanner = new BarcodeScanner(decoder, new MyBarcodeDecoderListener()); 
      _barcodeScreen = new MyBarcodeScannerViewScreen(_scanner); 

     } catch (Exception e) { 
      System.out.println("Could not initialize barcode scanner: " + e); 
      return; 
     } 
    } 

    // If we get here, all the barcode scanning infrastructure should be set 
    // up, so all we have to do is start the scan and display the viewfinder 
    try { 
     _scanner.startScan(); 
     UiApplication.getUiApplication().pushScreen(_barcodeScreen); 
    } catch (Exception e) { 
     System.out.println("Could not start scan: " + e); 
    } 

} 
/*** 
* MyBarcodeScannerViewScreen 
* <p> 
* This view screen is simply an extension of MainScreen that will hold our 
* scanner's viewfinder, and handle cleanly stopping the scan if the user 
* decides they want to abort via the back button. 
* 
* @author PBernhardt 
* 
*/ 
private class MyBarcodeScannerViewScreen extends MainScreen { 

    public MyBarcodeScannerViewScreen(BarcodeScanner scanner) { 
     super(); 
     try { 
      // Get the viewfinder and add it to the screen 
      _scanner.getVideoControl().setDisplayFullScreen(true); 
      Field viewFinder = _scanner.getViewfinder(); 
      this.add(viewFinder); 

      // Create and add our key listener to the screen 
      this.addKeyListener(new MyKeyListener()); 

     } catch (Exception e) { 
      System.out.println("Error creating view screen: " + e); 
     } 

    } 

    /*** 
    * MyKeyListener 
    * <p> 
    * This KeyListener will stop the current scan cleanly when the back 
    * button is pressed, and then pop the viewfinder off the stack. 
    * 
    * @author PBernhardt 
    * 
    */ 
    private class MyKeyListener implements KeyListener { 

     public boolean keyDown(int keycode, int time) { 

      // First convert the keycode into an actual key event, taking 
      // modifiers into account 
      int key = Keypad.key(keycode); 

      // From there we can compare against the escape key constant. If 
      // we get it, we stop the scan and pop this screen off the stack 
      if (key == Keypad.KEY_ESCAPE) { 
       try { 
        _scanner.stopScan(); 
       } catch (Exception e) { 
        System.out.println("Error stopping scan: " + e); 
       } 
       UiApplication.getUiApplication().invokeLater(new Runnable() { 
        public void run() { 
         UiApplication.getUiApplication().popScreen(_barcodeScreen); 

        } 
       }); 

       return true; 

      } 
      // Otherwise, we'll return false so as not to consume the 
      // keyDown event 
      return false; 
     } 

     // We will only act on the keyDown event 
     public boolean keyChar(char key, int status, int time) { 
      return false; 
     } 

     public boolean keyRepeat(int keycode, int time) { 
      return false; 
     } 

     public boolean keyStatus(int keycode, int time) { 
      return false; 
     } 

     public boolean keyUp(int keycode, int time) { 
      return false; 
     } 

    } 
} 
/*** 
* MyBarcodeDecoderListener 
* <p> 
* This BarcodeDecoverListener implementation tries to open any data encoded 
* in a barcode in the browser. 
* 
* @author PBernhardt 
* 
**/ 
private class MyBarcodeDecoderListener implements BarcodeDecoderListener { 

    public void barcodeDecoded(final String rawText) { 

     // First pop the viewfinder screen off of the stack so we can see 
     // the main app 
     UiApplication.getUiApplication().invokeLater(new Runnable() { 
      public void run() { 
       UiApplication.getUiApplication().popScreen(_barcodeScreen); 
      } 
     }); 

     _barcodeScreen.invalidate(); 
     //Display this barcode on LabelField on BarcodeScanSample MainScreen we can also set whatever field here. 
     UiApplication.getUiApplication().invokeLater(new Runnable() { 
      public void run() { 
        UiApplication.getUiApplication().popScreen(); 
        UiApplication.getUiApplication().pushScreen(new BarcodeScanSample(rawText)); 
        _barcodeScreen.close(); 
        _barcodeScreen=null; 
      } 
     }); 
    } 

} 

}