IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Webcam impossible !


Sujet :

C#

  1. #1
    Membre du Club
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2014
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2014
    Messages : 53
    Points : 54
    Points
    54
    Par défaut Webcam impossible !
    Bonjour !

    J'aimerais lire des codes barres (EAN) via la webcam du poste, cependant je n'arrive pas à récupérer l'image de ma WebCam, celle-ci m'affiche un fond vert et rien d'autre. (Ma Webcam fonctionne bien)

    Pour afficher ma Webcam j'utilise une classe récupéré dans la librairie ZXing, la classe Webcam suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    using System;
    using System.Collections;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
     
    namespace WebCamTest
    {
       public class WebCam : IDisposable
       {
          private const int WM_CAP = 0x400;
          private const int WM_CAP_DRIVER_CONNECT = 0x40a;
          private const int WM_CAP_DRIVER_DISCONNECT = 0x40b;
          private const int WM_CAP_EDIT_COPY = 0x41e;
          private const int WM_CAP_GET_FRAME = 1084;
          private const int WM_CAP_COPY = 1054;
          private const int WM_CAP_SET_PREVIEW = 0x432;
          private const int WM_CAP_SET_OVERLAY = 0x433;
          private const int WM_CAP_SET_PREVIEWRATE = 0x434;
          private const int WM_CAP_SET_SCALE = 0x435;
          private const int WS_CHILD = 0x40000000;
          private const int WS_VISIBLE = 0x10000000;
          private const int SWP_NOMOVE = 0x2;
          private const int SWP_NOZORDER = 0x4;
          private const int HWND_BOTTOM = 1;
     
          //This function enables enumerate the web cam devices
          [DllImport("avicap32.dll")]
          protected static extern bool capGetDriverDescriptionA(short wDriverIndex,
              [MarshalAs(UnmanagedType.VBByRefStr)]ref String lpszName,
             int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref String lpszVer, int cbVer);
     
          //This function enables create a  window child with so that you can display it in a picturebox for example
          [DllImport("avicap32.dll")]
          protected static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName,
              int dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int nID);
     
          //This function enables set changes to the size, position, and Z order of a child window
          [DllImport("user32")]
          protected static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
     
          //This function enables send the specified message to a window or windows
          [DllImport("user32", EntryPoint = "SendMessageA")]
          protected static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
     
          //This function enable destroy the window child 
          [DllImport("user32")]
          protected static extern bool DestroyWindow(int hwnd);
     
          // Normal device ID
          int DeviceID = 0;
          // Handle value to preview window
          int hHwnd = 0;
          //The devices list
          ArrayList ListOfDevices = new ArrayList();
     
          //The picture to be displayed
          public PictureBox Container { get; set; }
     
          // Connect to the device.
          /// <summary>
          /// This function is used to load the list of the devices 
          /// </summary>
          public void Load()
          {
             string Name = String.Empty.PadRight(100);
             string Version = String.Empty.PadRight(100);
             bool moreDevices;
             short index = 0;
     
             // Load name of all avialable devices into the lstDevices .
             do
             {
                // Get Driver name and version
                moreDevices = capGetDriverDescriptionA(index, ref Name, 100, ref Version, 100);
                // If there was a device add device name to the list
                if (moreDevices)
                   ListOfDevices.Add(Name.Trim());
                index += 1;
             }
             while (moreDevices);
          }
     
          /// <summary>
          /// Function used to display the output from a video capture device, you need to create 
          /// a capture window.
          /// </summary>
          public void OpenConnection()
          {
             string DeviceIndex = Convert.ToString(DeviceID);
             IntPtr oHandle = Container.Handle;
     
             // Open Preview window in picturebox .
             // Create a child window with capCreateCaptureWindowA so you can display it in a picturebox.
     
             hHwnd = capCreateCaptureWindowA(ref DeviceIndex, WS_VISIBLE | WS_CHILD, 0, 0, 640, 480, oHandle.ToInt32(), 0);
     
             // Connect to device
             if (SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 0) != 0)
             {
                // Set the preview scale
                SendMessage(hHwnd, WM_CAP_SET_SCALE, -1, 0);
                // Set the preview rate in terms of milliseconds
                SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 100, 0);
                // Start previewing the image from the camera
                SendMessage(hHwnd, WM_CAP_SET_PREVIEW, -1, 0);
                // Resize window to fit in picturebox
                SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, Container.Width, Container.Height, SWP_NOMOVE | SWP_NOZORDER);
             }
             else
             {
                // Error connecting to device close window
                DestroyWindow(hHwnd);
             }
          }
     
          void CloseConnection()
          {
             SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 0);
             // close window
             DestroyWindow(hHwnd);
          }
     
          public Bitmap GetCurrentImage()
          {
             // get the next frame;
             SendMessage(hHwnd, WM_CAP_GET_FRAME, 0, 0);
     
             // copy the frame to the clipboard
             SendMessage(hHwnd, WM_CAP_COPY, 0, 0);
     
             // Get image from clipboard and convert it to a bitmap
             IDataObject data = Clipboard.GetDataObject();
             if (data.GetDataPresent(typeof(Bitmap)))
             {
                var oImage = (Bitmap)data.GetData(typeof(Bitmap));
                Container.Image = oImage;
                return oImage;
             }
     
             return null;
          }
     
          ~WebCam()
          {
             Dispose(false);
             GC.SuppressFinalize(this);
          }
     
          public void Dispose()
          {
             Dispose(true);
          }
     
          virtual protected void Dispose(bool disposing)
          {
             CloseConnection();
          }
       }
    }
    Et le code de ma page :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
     
    using ZXing;
     
    namespace WebCamTest
    {
        public partial class Form1 : Form
        {
            private WebCam wCam;
            private Timer webCamTimer;
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void btn_Cam_Click(object sender, EventArgs e)
            {
                if (wCam == null)
                {
                    wCam = new WebCam { Container = BoxCam };
     
                    wCam.OpenConnection();
     
                    webCamTimer = new Timer();
                    webCamTimer.Tick += webCamTimer_Tick;
                    webCamTimer.Interval = 200;
                    webCamTimer.Start();
                }
                else
                {
                    webCamTimer.Stop();
                    webCamTimer = null;
                    wCam.Dispose();
                    wCam = null;
                }
            }
     
            void webCamTimer_Tick(object sender, EventArgs e)
            {
                var bitmap = wCam.GetCurrentImage();
                if (bitmap == null)
                    return;
                var reader = new BarcodeReader();
                var result = reader.Decode(bitmap);
                if (result != null)
                {
                    /*txtTypeWebCam.Text = result.BarcodeFormat.ToString();
                    txtContentWebCam.Text = result.Text;*/
                }
            }
        }
    }
    Actuellement quand je clique sur mon bouton, il me fait choisir mon périphérique, (Integrated Webcam) je valide et rien ne se passe, je ne sais pas réellement ou chercher si quelqu'un à une idée pour ce soucis, je suis preneur !

    Merci d'avance,

    Krishnak

  2. #2
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2008
    Messages
    337
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2008
    Messages : 337
    Points : 456
    Points
    456
    Par défaut WebCam_Capture
    Bonjour,

    En pièce jointe tu trouvera un exemple je n'ai pas utilisé ZXing mais une autre librairie assez facile à utilisé.

    Bonne continuation.
    Fichiers attachés Fichiers attachés

Discussions similaires

  1. Impossible de récupérer ma webcam dans une applet
    Par drasalmed dans le forum Applets
    Réponses: 0
    Dernier message: 07/01/2011, 16h29
  2. [ArchiveBuilder][JavaMail] exécution impossible...
    Par Gorthal dans le forum JBuilder
    Réponses: 7
    Dernier message: 10/01/2003, 09h12
  3. Réponses: 8
    Dernier message: 20/11/2002, 11h50
  4. Installation de ma Webcam Philips
    Par kensou.8 dans le forum Matériel
    Réponses: 2
    Dernier message: 23/10/2002, 20h32
  5. webcam : lire sur un port usb en c/c++ ou java. sous win. ?
    Par flo007 dans le forum Choisir un environnement de développement
    Réponses: 2
    Dernier message: 24/05/2002, 23h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo