package com.dynamsoft.server;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.imageio.ImageIO;

import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;

import com.dynamsoft.barcode.EnumBarCode;
import com.dynamsoft.barcode.JBarcode;
import com.dynamsoft.barcode.tagBarcodeResult;
import com.dynamsoft.barcode.tagBarcodeResultArray;

@WebSocket
public class BarcodeSocket {

	private Session mSession;
	private String mFile;
	
	public BarcodeSocket() {
		// TODO Auto-generated constructor stub
		mFile = this + ".png";
	}
	
    @OnWebSocketClose
    public void onClose(int statusCode, String reason) {
    	// Delete image cache
        File file = new File(mFile);
        boolean isDeleted = file.delete();
        System.out.println("File " + mFile + " deleted: " + isDeleted);
    }

    @OnWebSocketError
    public void onError(Throwable t) {
        System.out.println("Error: " + t.getMessage());
    }

    @OnWebSocketConnect
    public void onConnect(Session session) {
    	mSession = session;
        System.out.println("Connect: " + session.getRemoteAddress().getAddress());
    }
    
    @OnWebSocketMessage
    public void onMessage(byte[] data, int off, int len) {
    	ByteArrayInputStream in = new ByteArrayInputStream(data);
    	BufferedImage bi;
		try {
			bi = ImageIO.read(in);
			File file = new File(mFile);
			// save client data to local image file
			ImageIO.write(bi, "PNG", file);
			// read barcode
			String r = readBarcode(mFile);
			
			if (r.equals("")) {
				System.out.println("No barcode detected.");
				mSession.getRemote().sendString("No barcode detected. " + new Date());
			}
			else {
				mSession.getRemote().sendString(r);
				System.out.println(r);
			}
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    }
    
    
    @OnWebSocketMessage
    public void onMessage(String message) {
    	System.out.println("Received message: "+message);
    }

    private String readBarcode(String fileName) {
    	// Set DBR license
		JBarcode.DBR_InitLicense("38B9B94D8B0E2B41DB1CC80A58946567");
		// Result array
		tagBarcodeResultArray paryResults = new tagBarcodeResultArray();
		// Read barcode
		int iret = JBarcode.DBR_DecodeFile(fileName, 100,
				EnumBarCode.OneD | EnumBarCode.QR_CODE
				| EnumBarCode.PDF417 | EnumBarCode.DATAMATRIX, paryResults);
		
		System.out.println("DBR_DecodeFile return value: " + iret);
		String r = "";
		for (int iIndex = 0; iIndex < paryResults.iBarcodeCount; iIndex++) {
			tagBarcodeResult result = paryResults.ppBarcodes[iIndex];
			int barcodeDataLen = result.iBarcodeDataLength;

			byte[] pszTemp1 = new byte[barcodeDataLen];
			for (int x = 0; x < barcodeDataLen; x++) {
				pszTemp1[x] = result.pBarcodeData[x];
			}

			r += "Value: " + new String(pszTemp1);
		}
		
		return r;
    }
}

@@ -618,27 +618,25 @@
 oft.
-b
+dbr.B
 arcode
-.EnumBarCo
+Rea
 de
+r
 ;%0Aim
@@ -658,71 +658,34 @@
 oft.
-barcode.JBarcode;%0Aimport com.dynamsoft.barcode.tagBarcodeResult
+dbr.BarcodeReaderException
 ;%0Aim
@@ -707,37 +707,22 @@
 oft.
-barcode.tagBarcode
+dbr.Text
 Result
-Array
 ;%0A%0A@
@@ -811,16 +811,44 @@
  mFile;%0A
+%09private BarcodeReader dbr;%0A
 %09%0A%09publi
@@ -909,16 +909,16 @@
 or stub%0A
-
 %09%09mFile 
@@ -934,16 +934,159 @@
 %22.png%22;%0A
+%09%09try %7B%0A%09%09%09dbr = new BarcodeReader();%0A%09%09%7D catch (BarcodeReaderException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A
 %09%7D%0A%09%0A   
@@ -2567,221 +2567,105 @@
    %09
-// Set DBR license%0A%09%09JBarcode.DBR_InitLicense(%2238B9B94D8B0E2B41DB1CC80A58946567%22);%0A%09%09// Result array%0A%09%09tagBarcodeResultArray paryResults = new tagBarcodeResultArray();%0A%09%09// Read barcode%0A%09%09int iret = JBarcode.DBR_D
+StringBuilder sb = new StringBuilder();%0A%09%09TextResult%5B%5D results = null;%0A%09%09try %7B%0A%09%09%09results = dbr.d
 ecod
@@ -2684,587 +2684,241 @@
 me, 
-100,%0A%09%09%09%09EnumBarCode.OneD %7C EnumBarCode.QR_CODE%0A%09%09%09%09%7C EnumBarCode.PDF417 %7C EnumBarCode.DATAMATRIX, paryResults);%0A%09%09%0A%09%09System.out.println(%22DBR_DecodeFile return value: %22 + iret);%0A%09%09String r = %22%22;%0A%09%09for (int iIndex = 0; iIndex %3C paryResults.iBarcodeCount; iIndex++) %7B%0A%09%09%09tagBarcodeResult result = paryResults.ppBarcodes%5BiIndex%5D;%0A%09%09%09int barcodeDataLen = result.iBarcodeDataLength;%0A%0A%09%09%09byte%5B%5D pszTemp1 = new byte%5BbarcodeDataLen%5D;%0A%09%09%09for (int x = 0; x %3C barcodeDataLen; x++) %7B%0A%09%09%09%09pszTemp1%5Bx%5D = 
+%22%22);%0A%09%09%7D catch (BarcodeReaderException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09for (TextResult result : results) %7B%0A%09%09%09sb.append(%22Value: %22);%0A%09%09%09sb.append(
 result.
-pB
+b
 arcode
-Data%5Bx%5D
+Text)
 ;%0A
-%09
 %09%09%7D%0A
-%0A%09
 %09%09r
- += %22Value: %22 + new String(pszTemp1);%0A%09%09%7D%0A%09%09%0A%09%09return r
+eturn sb.toString()
 ;%0A  

use DBR java