DZ-DEVELOPER Ответов: 1

Как конвертировать весь файл проекта для VB


using System;
using System.Drawing;
using System.Windows.Forms;

using Dynamsoft.Barcode;
using TouchlessLib;
using System.Diagnostics;

namespace BarcodeReaderApp
{
    public partial class Form1 : Form
    {
        private BarcodeReader _barcodeReader;
        private TouchlessMgr _touch;
        private const int _previewWidth = 640;
        private const int _previewHeight = 480;

        public Form1()
        {
            InitializeComponent();

            // Initialize Dynamsoft Barcode Reader
            _barcodeReader = new BarcodeReader();
            // Initialize Touchless
            _touch = new TouchlessMgr();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Title = "Open Image";
                //dlg.Filter = "bmp files (*.bmp)|*.bmp";

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Bitmap bitmap = null;
                    
                    try
                    {
                        bitmap =  new Bitmap(dlg.FileName);
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show("File not supported.");
                        return;
                    }

                    pictureBox1.Image = new Bitmap(dlg.FileName);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                MessageBox.Show("Please load an image!");
                return;
            }

            ReadBarcode((Bitmap)pictureBox1.Image);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string button_text = button3.Text;
            if (button_text.Equals("Start Webcam"))
            {
                button3.Text = "Stop Webcam";
                StartCamera();
            }
            else
            {
                button3.Text = "Start Webcam";
                StopCamera();
            }  
        }

        private void StartCamera()
        {
            if (_touch.Cameras.Count == 0)
            {
                MessageBox.Show("No USB webcam connected");
                button3.Text = "Start Webcam";
                return;
            }            

            // Start to acquire images
            _touch.CurrentCamera = _touch.Cameras[0];
            _touch.CurrentCamera.CaptureWidth = _previewWidth; // Set width
            _touch.CurrentCamera.CaptureWidth = _previewHeight; // Set height
            _touch.CurrentCamera.OnImageCaptured += new EventHandler<cameraeventargs>(OnImageCaptured); // Set preview callback function
        }

        private void StopCamera()
        {
            button3.Text = "Start Webcam";
            if (_touch.CurrentCamera != null)
            {
                _touch.CurrentCamera.OnImageCaptured -= new EventHandler<cameraeventargs>(OnImageCaptured);
            }
        }

        private void OnImageCaptured(object sender, CameraEventArgs args)
        {
            // Get the bitmap
            Bitmap bitmap = args.Image;

            // Read barcode and show results in UI thread
            this.Invoke((MethodInvoker)delegate
            {
                pictureBox1.Image = bitmap;
                ReadBarcode(bitmap);
            });
        }

        private void ReadBarcode(Bitmap bitmap)
        {
            // Read barcodes with Dynamsoft Barcode Reader
            Stopwatch sw = Stopwatch.StartNew();
            sw.Start();
            BarcodeResult[] results = _barcodeReader.DecodeBitmap(bitmap);
            sw.Stop();
            Console.WriteLine(sw.Elapsed.TotalMilliseconds + "ms");

            // Clear previous results
            textBox1.Clear();

            if (results == null)
            {
                textBox1.Text = "No barcode detected!";
                return;
            }

            // Display barcode results
            foreach (BarcodeResult result in results)
            {                
                textBox1.AppendText(result.BarcodeText + "\n");
                textBox1.AppendText("\n");
            }
        }
    }
}


Что я уже пробовал:

Public Class Form2
    Inherits Form
    Private _barcodeReader As BarcodeReader
    Private _touch As TouchlessMgr
    Private Const _previewWidth As Integer = 640
    Private Const _previewHeight As Integer = 480
    Public Sub New()
        InitializeComponent()

        ' Initialize Dynamsoft Barcode Reader
        _barcodeReader = New BarcodeReader()
        ' Initialize Touchless
        _touch = New TouchlessMgr()
    End Sub


    Private Sub AboutThisApplicationToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutThisApplicationToolStripMenuItem.Click
        AboutBox2.Show()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myFileDlog As New OpenFileDialog()
        myFileDlog.InitialDirectory = "c:\"
        myFileDlog.Filter = "All Files (*.*)|*.*" &
            "|Image Files (*.jpg)|*.jpg" &
            "|Image Files (*.bmp)|*.bmp" &
            "|Image Files (*.png)|*.png"
        myFileDlog.FilterIndex = 1
        myFileDlog.RestoreDirectory = True
        If myFileDlog.ShowDialog() = DialogResult.OK Then
            PictureBox87.Image = Image.FromFile(myFileDlog.FileName)
        End If
        TextBox1.Text = myFileDlog.FileName
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If PictureBox87.Image Is Nothing Then
            MessageBox.Show("Please load an image!")
            Return
        End If

        ReadBarcode(DirectCast(PictureBox87.Image, Bitmap))
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim button_text As String = Button2.Text
        If button_text.Equals("Start Webcam") Then
            Button2.Text = "Stop Webcam"
            StartCamera()
        Else
            Button2.Text = "Start Webcam"
            StopCamera()
        End If
    End Sub
    Private Sub StartCamera()
        'Form2._touch As touchlessmgr.crrentCamera As Camera
        'Event camera().OnImageCaptured As eventhandler(of cameraeventargs)
        If _touch.Cameras.Count = 0 Then
            MessageBox.Show("No USB webcam connected")
            Button2.Text = "Start Webcam"
            Return
        End If

        ' Start to acquire images
        _touch.CurrentCamera = _touch.Cameras(0)
        _touch.CurrentCamera.CaptureWidth = _previewWidth
        ' Set width
        _touch.CurrentCamera.CaptureWidth = _previewHeight
        ' Set height
        _touch.CurrentCamera.OnImageCaptured += New EventHandler(Of CameraEventArgs)(AddressOf OnImageCaptured)
        ' Set preview callback function
    End Sub
    Private Sub StopCamera()
        Button2.Text = "Start Webcam"
        If _touch.CurrentCamera IsNot Nothing Then
            '_touch.CurrentCamera.OnImageCaptured -= New EventHandler(Of CameraEventArgs)(AddressOf OnImageCaptured)
        End If
    End Sub

    Private Sub OnImageCaptured(sender As Object, args As CameraEventArgs)
        ' Get the bitmap
        Dim bitmap As Bitmap = args.Image

        ' Read barcode and show results in UI thread
        Me.Invoke(DirectCast(Sub()
                                 PictureBox87.Image = bitmap
                                 ReadBarcode(bitmap)

                             End Sub, MethodInvoker))
    End Sub

    Private Sub ReadBarcode(bitmap As Bitmap)
        ' Read barcodes with Dynamsoft Barcode Reader
        Dim sw As Stopwatch = Stopwatch.StartNew()
        sw.Start()
        Dim results As BarcodeResult() = _barcodeReader.DecodeBitmap(bitmap)
        sw.[Stop]()
        Console.WriteLine(sw.Elapsed.TotalMilliseconds + "ms")

        ' Clear previous results
        RichTextBox1.Clear()

        If results Is Nothing Then
            RichTextBox1.Text = "No barcode detected!"
            Return
        End If

        ' Display barcode results
        For Each result As BarcodeResult In results
            RichTextBox1.AppendText(result.BarcodeText + vbLf)
            RichTextBox1.AppendText(vbLf)
        Next
    End Sub

End Class


Экран Печати:
[Проект экрана изображения]

Graeme_Grant

И что говорит вам красная волнистая линия, когда вы зависаете над ней?

1 Ответов

Рейтинг:
10

Graeme_Grant

Прошло уже некоторое время, но с моей точки зрения, в VB вам нужно сделать что-то вроде:

AddHandler _touch.CurrentCamera.OnImageCaptured, AddressOf OnImageCaptured()