2016-03-24 139 views
3

首先,我閱讀了所有這些主題,瞭解如何在Java中使用Zxing,但總是在com.google.zxing.client.j2se丟失時出錯。 zxing core-3.2.1.jar在eclipse和所有其他zxing軟件包工作除非j2se)或剛剛找到創建qr圖像的解決方案...查找圖像中的QR碼並使用Zxing對其進行解碼

我的目標是編寫一個單一的方法,獲取圖像文件找到qr在此圖像中碼,解碼QR碼,並返回字符串,基本上就應該是這樣的:

import com.google.zxing.*; 

public class QRCode { 

    /* 
    * ... 
    */ 

    public String getDecodedString(SomeStandardImageType photo){ 
     // detect the qr code in a photo 
     // create qr image from detected area in photo 
     // decode the new created qr image and return the string 
     return "This is the decoded dataString from the qr code in the photo"; 
    } 

} 

綜上所述方法應該得到的圖像文件,如後續荷蘭國際集團

enter image description here

,而應返回的URL,或者如果只是失敗 「」。

該代碼應該與Zxing 3.2.1兼容。

編輯:問題已解決。對於對此感興趣的其他人,我想說,將外部罐子core-3.2.1.jarjavase-3.2.1.jar添加到外部罐子是很重要的。我的答案沒有後者,但取決於Android圖像庫。

+0

什麼你的問題? –

+0

問題是我怎樣才能使用Zxing 3.2.1的算法;另一個方向顯示在這裏:http://crunchify.com/java-simple-qr-code-generator-example/ – Tim

回答

2

這裏是

  1. 你需要構建斑馬線庫中的代碼從QR代碼創建QR碼和閱讀郵件

  2. 主要描述上的QR碼的創建和QR碼提取

    package com.attendance.mark; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.IOException; 
    import java.util.HashMap; 
    import java.util.Map; 
    
    import javax.imageio.ImageIO; 
    
    import com.google.zxing.BarcodeFormat; 
    import com.google.zxing.BinaryBitmap; 
    import com.google.zxing.EncodeHintType; 
    import com.google.zxing.MultiFormatReader; 
    import com.google.zxing.MultiFormatWriter; 
    import com.google.zxing.NotFoundException; 
    import com.google.zxing.Result; 
    import com.google.zxing.WriterException; 
    import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 
    import com.google.zxing.client.j2se.MatrixToImageWriter; 
    import com.google.zxing.common.BitMatrix; 
    import com.google.zxing.common.HybridBinarizer; 
    import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 
    
    public class QRCode { 
    
        /** 
        * 
        * @param args 
        * @throws WriterException 
        * @throws IOException 
        * @throws NotFoundException 
        */ 
        public static void main(String[] args) throws WriterException, IOException, 
         NotFoundException { 
        String qrCodeData = "student3232_2015_12_15_10_29_46_123"; 
        String filePath = "F:\\Opulent_ProjectsDirectory_2015-2016\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\AttendanceUsingQRCode\\QRCodes\\student3232_2015_12_15_10_29_46_123"; 
        String charset = "UTF-8"; // or "ISO-8859-1" 
        Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new HashMap<EncodeHintType, ErrorCorrectionLevel>(); 
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 
    
        createQRCode(qrCodeData, filePath, charset, hintMap, 200, 200); 
        System.out.println("QR Code image created successfully!"); 
    
        System.out.println("Data read from QR Code: " 
         + readQRCode(filePath, charset, hintMap)); 
    
        } 
    
        /*** 
        * 
        * @param qrCodeData 
        * @param filePath 
        * @param charset 
        * @param hintMap 
        * @param qrCodeheight 
        * @param qrCodewidth 
        * @throws WriterException 
        * @throws IOException 
        */ 
        public static void createQRCode(String qrCodeData, String filePath, 
         String charset, Map hintMap, int qrCodeheight, int qrCodewidth) 
         throws WriterException, IOException { 
        BitMatrix matrix = new MultiFormatWriter().encode(
         new String(qrCodeData.getBytes(charset), charset), 
         BarcodeFormat.QR_CODE, qrCodewidth, qrCodeheight); 
        MatrixToImageWriter.writeToFile(matrix, filePath.substring(filePath 
         .lastIndexOf('.') + 1), new File(filePath)); 
        } 
    
        /** 
        * 
        * @param filePath 
        * @param charset 
        * @param hintMap 
        * 
        * @return Qr Code value 
        * 
        * @throws FileNotFoundException 
        * @throws IOException 
        * @throws NotFoundException 
        */ 
        public static String readQRCode(String filePath, String charset, Map hintMap) 
         throws FileNotFoundException, IOException, NotFoundException { 
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
         new BufferedImageLuminanceSource(
          ImageIO.read(new FileInputStream(filePath))))); 
        Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap); 
        return qrCodeResult.getText(); 
        } 
    } 
    
+0

謝謝。你的代碼是使用eclipse進行開發的好基礎,因爲你不使用android庫。如果'javase-3.2.1.jar'被添加到外部罐子中,則這可以使用Zxing 3.2.1。 – Tim

3

此代碼適合我。希望它可以幫助剛剛導入必要的軟件包,它應該工作

public class QR_Reader extends JFrame implements Runnable, ThreadFactory { 

private static final long serialVersionUID = 6441489157408381878L; 

private Executor executor = Executors.newSingleThreadExecutor(this); 

private Webcam webcam = null; 
private WebcamPanel panel = null; 
String s; 

public QR_Reader() { 
    super(); 
    setLayout(new FlowLayout()); 
    setTitle("Reading QR Code"); 
    Dimension size = WebcamResolution.QVGA.getSize(); 
    webcam = Webcam.getWebcams().get(0); 
    webcam.setViewSize(size); 
    panel = new WebcamPanel(webcam); 
    panel.setPreferredSize(size); 
    add(panel); 
    pack(); 
    setVisible(true); 
    setResizable(false); 
    executor.execute(this); 
} 
@Override 
public void run() { 

    do { 
     try { 
      Thread.sleep(100); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

     Result result = null; 
     BufferedImage image = null; 

     if (webcam.isOpen()) { 

      if ((image = webcam.getImage()) == null) { 
       continue; 
      } 

      LuminanceSource source = new BufferedImageLuminanceSource(image); 
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 

      try { 
       result = new MultiFormatReader().decode(bitmap); 
      } catch (NotFoundException e) { 
       // fall thru, it means there is no QR code in image 
      } 
     } 

     if (result != null) { 
      String time_then=result.getText(); //this is the text extracted from QR CODE 
      webcam.close(); 
      this.setVisible(false); 
      this.dispose(); 
      try { 
       new Compare().C_Main(time_then); 
      } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } while (true); 
} 

@Override 
public Thread newThread(Runnable r) { 
    Thread t = new Thread(r, "example-runner"); 
    t.setDaemon(true); 
    return t; 
} 

void QRC_Main() { 
    new QR_Reader(); 
} 
} 
+0

感謝您的回答,但至少'BufferedImageLuminanceSource'不適用於'Zxing 3.2.1'' – Tim

+0

我使用[此jar](http://mvnrepository.com/artifact/com.google.zxing/core/2.2)閱讀qrcode和[that](http://www.java2s.com/Code/Jar/) w/Downloadwebcamcapture033jar.htm)用網絡攝像頭進行圖像捕捉。它必須包括所有的類 –

+0

這是2.2.0版本,不是3.2.1然而,我很感謝你的答案,因爲你想幫助。其實在完整的Stackoverflow中是沒有用的3.2.1版本。 *創建* qr代碼的唯一工作代碼是從crunshify,但我想要相反的方向... – Tim

2

我現在讀深入斑馬線和下面的代碼將與斑馬線V3.2.1工作(此代碼的工作,而不javase LIB)

// Imports 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 

import com.google.zxing.BinaryBitmap; 
import com.google.zxing.ChecksumException; 
import com.google.zxing.FormatException; 
import com.google.zxing.LuminanceSource; 
import com.google.zxing.NotFoundException; 
import com.google.zxing.RGBLuminanceSource; 
import com.google.zxing.Reader; 
import com.google.zxing.Result; 
import com.google.zxing.common.HybridBinarizer; 
import com.google.zxing.qrcode.QRCodeReader; 

// Interesting method 
public static String decodeQRImage(String path) { 
    Bitmap bMap = BitmapFactory.decodeFile(path); 
    String decoded = null; 

    int[] intArray = new int[bMap.getWidth() * bMap.getHeight()]; 
    bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), 
      bMap.getHeight()); 
    LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), 
      bMap.getHeight(), intArray); 
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); 

    Reader reader = new QRCodeReader(); 
    try { 
     Result result = reader.decode(bitmap); 
     decoded = result.getText(); 
    } catch (NotFoundException e) { 
     e.printStackTrace(); 
    } catch (ChecksumException e) { 
     e.printStackTrace(); 
    } catch (FormatException e) { 
     e.printStackTrace(); 
    } 
    return decoded; 
}