2016-09-23 10 views
2

QRGeneratorと呼ばれる「静的クラス」があります。その目的はBitMatrixオブジェクトを生成し、そこからBufferedImageオブジェクトを生成することです。これはそれのためのコードです:BitMatrixオブジェクトでHashMapをシリアル化するにはどうすればよいですか? (QRコード/ zxing)

package ericsonwrp.republica.vintage.caixa; 

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.util.EnumMap; 
import java.util.Map; 

import com.google.zxing.BarcodeFormat; 
import com.google.zxing.EncodeHintType; 
import com.google.zxing.WriterException; 
import com.google.zxing.common.BitMatrix; 
import com.google.zxing.qrcode.QRCodeWriter; 
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 

public class QRGenerator { 

    private static BufferedImage image = null; 
    private static int size = 250; 
    private static BitMatrix byteMatrix = null; 

    public static BitMatrix generateBitMatrix(String codeText) { 
     try { 
      Map<EncodeHintType, Object> hintMap = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); 
      hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 
      hintMap.put(EncodeHintType.MARGIN, 1); 
      hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); 
      QRCodeWriter qrCodeWriter = new QRCodeWriter(); 
      byteMatrix = qrCodeWriter.encode(codeText, BarcodeFormat.QR_CODE, size, 
        size, hintMap); 
     } catch (WriterException e) { 
      e.printStackTrace(); 
     } 
     return byteMatrix; 
    } 

    public static BufferedImage generateImage(BitMatrix byteMatrix) { 
     int width = byteMatrix.getWidth(); 
     image = new BufferedImage(width, width, 
       BufferedImage.TYPE_INT_RGB); 
     image.createGraphics(); 
     Graphics2D graphics = (Graphics2D) image.getGraphics(); 
     graphics.setColor(Color.WHITE); 
     graphics.fillRect(0, 0, width, width); 
     graphics.setColor(Color.BLACK); 
     for (int i = 0; i < width; i++) { 
      for (int j = 0; j < width; j++) { 
       if (byteMatrix.get(i, j)) { 
        graphics.fillRect(i, j, 1, 1); 
       } 
      } 
     } 
     return image; 
    } 

} 

私がその目的である私のアプリケーションの「コンテンツ」を保存するために独自のファイルを定義するための、およびシリアル化する方法を提供すること「RepublicaVintageFile」と呼ばれる別のクラスを、(持っており、読んでください、これは質問には関係ありません)この "内容"。これはそれのためのコードです:

package ericsonwrp.republica.vintage.caixa; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectOutput; 
import java.io.ObjectOutputStream; 
import java.util.HashMap; 

public class RepublicaVintageFile { 

    private HashMap<String, Object> content; 

    public RepublicaVintageFile() { 
     setContent(new HashMap<String, Object>()); 
    } 

    public void serializeContent(String path) throws IOException { 
     FileOutputStream fout = new FileOutputStream(path, true); 
     ObjectOutput out = null; 
     try { 
      out = new ObjectOutputStream(fout); 
      out.writeObject(getContent()); 
     } finally { 
      try { 
      if (out != null) { 
       out.close(); 
      } 
      } catch (IOException ex) { 
      // ignore close exception 
      } 
      try { 
      fout.close(); 
      } catch (IOException ex) { 
      // ignore close exception 
      } 
     } 
    } 

    public HashMap<String, Object> getContent() { 
     return content; 
    } 

    public void setContent(HashMap<String, Object> content) { 
     this.content = content; 
    } 

} 

私のアプリケーションの「コンテンツ」とは、異なるファイル(private HashMap<String, BitMatrix> qrItems;)にHashMapの内側のラベルの下に保存されているBitMatrixオブジェクトが含まれます。最終的なBufferedImageをシリアル化するとjava.io.NotSerializableExceptionになるので、私はBitMatrixオブジェクトをシリアル化しようとしました。しかし、私の失望のために、それも不可能です。これは、スタックトレースです:

このすべてを考慮
java.io.NotSerializableException: com.google.zxing.common.BitMatrix 
    at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
    at java.io.ObjectOutputStream.writeObject(Unknown Source) 
    at java.util.HashMap.internalWriteEntries(Unknown Source) 
    at java.util.HashMap.writeObject(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source) 
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
    at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
    at java.io.ObjectOutputStream.writeObject(Unknown Source) 
    at java.util.HashMap.internalWriteEntries(Unknown Source) 
    at java.util.HashMap.writeObject(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source) 
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) 
    at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
    at java.io.ObjectOutputStream.writeObject(Unknown Source) 
    at ericsonwrp.republica.vintage.caixa.RepublicaVintageFile.serializeContent(RepublicaVintageFile.java:26) 
    at ericsonwrp.republica.vintage.caixa.MainFrame.openSaveAsDialog(MainFrame.java:204) 
    at ericsonwrp.republica.vintage.caixa.MainFrame.access$1(MainFrame.java:177) 
    at ericsonwrp.republica.vintage.caixa.MainFrame$4.actionPerformed(MainFrame.java:112) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
    at javax.swing.AbstractButton.doClick(Unknown Source) 
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) 
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source) 
    at java.awt.Component.processMouseEvent(Unknown Source) 
    at javax.swing.JComponent.processMouseEvent(Unknown Source) 
    at java.awt.Component.processEvent(Unknown Source) 
    at java.awt.Container.processEvent(Unknown Source) 
    at java.awt.Component.dispatchEventImpl(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
    at java.awt.Container.dispatchEventImpl(Unknown Source) 
    at java.awt.Window.dispatchEventImpl(Unknown Source) 
    at java.awt.Component.dispatchEvent(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.awt.EventQueue$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 

、私は私の質問に来る:どのように私はBitMatrixオブジェクトでHashMapをシリアライズすることができますか?フォルダに100個(またはそれ以上)の別々の.pngファイルを保存する必要はなく、常にコンテンツを取得するためにイメージを読み取る必要があるため、重要です。

+0

画像は私がそれについて読むことを思い出す限り、(Javaの華麗な「シリアライザ」で)シリアル化されていません – gpasch

答えて

0

私は、BitMatrixオブジェクトをブール2次元配列に変換する「保存」と「読み込み」することができました。

private HashMap<String, BitMatrix> qrMatrixes; 
private HashMap<String, boolean[][]> qrMatrixBooleanArrays; 

あなたが見ることができるように、私は、ブール[] []の形式で同等で、<文字列、BitMatrix>のHashMapを持っています。すなわち、(ブール[] []アレイに各BitMatrixオブジェクトからビットを転送する)私はこのような「翻訳」を作る方法は次のとおり

for (Map.Entry<String, BitMatrix> e : getQrMatrixes().entrySet()) { 
    boolean[][] b = new boolean[e.getValue().getHeight()][e.getValue().getWidth()]; 
    for (int j = 0; j < e.getValue().getHeight(); j++) { 
     for (int k = 0; k < e.getValue().getWidth(); k++) { 
      b[j][k] = e.getValue().get(j, k); 
     } 
    } 
    qrMatrixBooleanArrays.put(e.getKey(), b); 
} 

Javaのシリアライザは、そのようなオブジェクトをシリアル化(HashMapの<文字列、ブール[] []>)どのようにネストされていても問題はありません(私はそれが昔ながらの配列でプリミティブ型で動作すると想像していました)。ここでは、ファイルをロードした後、QRコードのリストを更新するために、全体の方法です。私はそれがより明確にするために少し難しい怖い

public void updateList() { 
     if (MainFrame.getCurrentFile() != null) { 
      setQrMatrixes(new HashMap<String, BitMatrix>()); 
      setQrMatrixBooleanArrays(new HashMap<String, boolean[][]>()); 
      for (Map.Entry<String, boolean[][]> e : ((HashMap<String, boolean[][]>) MainFrame.getCurrentFile().getContent().get("Qr Codes")).entrySet()) { 
       getQrMatrixBooleanArrays().put(e.getKey(), e.getValue()); 
      } 
      for (Map.Entry<String, boolean[][]> e : getQrMatrixBooleanArrays().entrySet()) { 
       BitMatrix b = new BitMatrix(e.getValue().length, e.getValue().length); 
       for (int i = 0; i < e.getValue().length; i++) { 
        for (int j = 0; j < e.getValue()[i].length; j++) { 
         if (e.getValue()[i][j] == true) { 
          b.set(i, j); 
         } 
        } 
       } 
       getQrMatrixes().put(e.getKey(), b); 
       qrListModel.addElement(e.getKey()); 
      } 
     } else { 
      System.out.println("No file is loaded."); 
     } 
    } 

。 BitMatrixオブジェクトの場合はExplore the docsとし、内容をプリミティブな2次元配列にコピーするだけです。がんばろう!

関連する問題