First, I am a c# new learner, so my question might be very simple one. I am trying to make a program that reading code 128(barcode form) and change this barcode to just regular letter. Even though I have installed ZXing.Net package using Nuget, there exists an red line on 'BarcodeReader'. How can I solve this probelm? The error shows "Using the generic type 'BarcodeReader' requires 1 type argument."
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using ZXing;
namespace KeyboardHook_test
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, uint threadId);
[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string lpFileName);
private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
const int WH_KEYBOARD_LL = 13;
const int WM_KEYDOWN = 0x0100;
const int VK_RETURN = 0x0D;
private IntPtr _hookID = IntPtr.Zero;
public Form1()
{
InitializeComponent();
SetHook();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
RemoveHook();
}
private string _currentBarcode = string.Empty;
private bool _isBarcodeInput = false;
private string ParseBarcode(string barcodeData)
{
// Parse the code 128 barcode data and convert it to text
// For example, you can use a library like ZXing.Net to parse the barcode data
var reader = new BarcodeReader
{
Options =
{
PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.CODE_128 }
},
AutoRotate = true
};
var result = reader.Decode(barcodeData);
if (result != null)
{
return result.Text;
}
return string.Empty;
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
if (vkCode == VK_RETURN) // check for the Enter key
{
if (_isBarcodeInput)
{
// Parse the barcode value and display it
string barcode = ParseBarcode(_currentBarcode);
MessageBox.Show("Barcode read: " + barcode);
_currentBarcode = string.Empty;
_isBarcodeInput = false;
}
}
else
{
// Add the input to the current barcode value
_currentBarcode += (char)vkCode;
_isBarcodeInput = true;
}
}
return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
}
private void SetHook()
{
using (Process currentProcess = Process.GetCurrentProcess())
using (ProcessModule currentModule = currentProcess.MainModule)
{
IntPtr hModule = LoadLibrary("User32.dll");
_hookID = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, hModule, 0);
}
}
private void RemoveHook()
{
UnhookWindowsHookEx(_hookID);
}
}
}