2016-04-04 12 views
5

Sto usando la libreria ZXing.Net per codificare e decodificare il mio file video usando RS Encoder. Funziona bene aggiungendo e rimuovendo la parità rispettivamente dopo la codifica e la decodifica. Ma quando si scrive un file decodificato, viene aggiunto "?" caratteri in file su posizioni diverse che non facevano parte del file originale. Non capisco perché questo problema si sta verificando durante la scrittura di file. Ecco il mio codiceInusuale aggiunta di caratteri dopo la scrittura del file decodificato

using ZXing.Common.ReedSolomon; 

namespace zxingtest 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      string inputFileName = @"D:\JM\bin\baseline_30.264"; 
      string outputFileName = @"D:\JM\bin\baseline_encoded.264"; 
      string Content = File.ReadAllText(inputFileName, ASCIIEncoding.Default); 
      //File.WriteAllText(outputFileName, Content, ASCIIEncoding.Default); 
      ReedSolomonEncoder enc = new ReedSolomonEncoder(GenericGF.AZTEC_DATA_12); 
      ReedSolomonDecoder dec = new ReedSolomonDecoder(GenericGF.AZTEC_DATA_12); 
      //string s = "1,2,4,6,1,7,4,0,0"; 
      //int[] array = s.Split(',').Select(str => int.Parse(str)).ToArray(); 
      int parity = 10; 
      List<byte> toBytes = ASCIIEncoding.Default.GetBytes(Content.Substring(0, 500)).ToList(); 

      for (int index = 0; index < parity; index++) 
      { 
       toBytes.Add(0); 
      } 
      int[] bytesAsInts = Array.ConvertAll(toBytes.ToArray(), c => (int)c); 

      enc.encode(bytesAsInts, parity); 
      bytesAsInts[1] = 3; 
      dec.decode(bytesAsInts, parity); 
      string st = new string(Array.ConvertAll(bytesAsInts.ToArray(), z => (char)z)); 
      File.WriteAllText(outputFileName, st, ASCIIEncoding.Default); 
     } 
    } 
} 

E qui è la vista dei file hex del flusso di bit H.264 enter image description here

risposta

2

Il problema è che si sta gestendo un formato binario, come se si tratta di un file di testo con una codifica. Ma basandoti su ciò che stai facendo, sembra che tu sia interessato solo a leggere alcuni byte, elaborarli (codificare, decodificare) e poi riportare i byte su un file.

Se è ciò che è necessario, utilizzare il lettore e lo scrittore appropriati per i file, in questo caso BinaryReader e BinaryWriter. Usando il tuo codice come punto di partenza questa è la mia versione che usa i lettori/scrittori menzionati in precedenza. Il mio file di input e il file di output sono simili per i byte letti e scritti.

string inputFileName = @"input.264"; 
string outputFileName = @"output.264"; 

ReedSolomonEncoder enc = new ReedSolomonEncoder(GenericGF.AZTEC_DATA_12); 
ReedSolomonDecoder dec = new ReedSolomonDecoder(GenericGF.AZTEC_DATA_12); 

const int parity = 10; 

// open a file as stream for reading 
using (var input = File.OpenRead(inputFileName)) 
{ 
    const int max_ints = 256; 
    int[] bytesAsInts = new int[max_ints]; 
    // use a binary reader 
    using (var binary = new BinaryReader(input)) 
    { 
     for (int i = 0; i < max_ints - parity; i++) 
     { 
      //read a single byte, store them in the array of ints 
      bytesAsInts[i] = binary.ReadByte(); 
     } 
     // parity 
     for (int i = max_ints - parity; i < max_ints; i++) 
     { 
      bytesAsInts[i] = 0; 
     } 

     enc.encode(bytesAsInts, parity); 

     bytesAsInts[1] = 3; 

     dec.decode(bytesAsInts, parity); 

     // create a stream for writing 
     using(var output = File.Create(outputFileName)) 
     { 
      // write bytes back 
      using(var writer = new BinaryWriter(output)) 
      { 
       foreach(var value in bytesAsInts) 
       { 
        // we need to write back a byte 
        // not an int so cast it 
        writer.Write((byte)value); 
       } 
      } 
     } 
    } 
} 
Problemi correlati