Created
October 14, 2024 09:23
-
-
Save btel/f38b408c6e99f4803c3c357de255f55d to your computer and use it in GitHub Desktop.
twain image acquire (copilot)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <twain.h> | |
// Function to initialize TWAIN | |
TW_UINT16 InitializeTwain(TW_IDENTITY *appId, TW_IDENTITY *sourceId) { | |
TW_UINT16 rc; | |
rc = DSM_Entry(appId, NULL, DG_CONTROL, DAT_PARENT, MSG_OPENDSM, (TW_MEMREF)NULL); | |
if (rc != TWRC_SUCCESS) return rc; | |
rc = DSM_Entry(appId, NULL, DG_CONTROL, DAT_IDENTITY, MSG_GETDEFAULT, sourceId); | |
if (rc != TWRC_SUCCESS) return rc; | |
rc = DSM_Entry(appId, NULL, DG_CONTROL, DAT_IDENTITY, MSG_OPENDS, sourceId); | |
return rc; | |
} | |
// Function to acquire image | |
TW_UINT16 AcquireImage(TW_IDENTITY *appId, TW_IDENTITY *sourceId) { | |
TW_UINT16 rc; | |
TW_IMAGEINFO imageInfo; | |
TW_IMAGEMEMXFER imageMemXfer; | |
rc = DSM_Entry(appId, sourceId, DG_IMAGE, DAT_IMAGENATIVEXFER, MSG_GET, &imageMemXfer); | |
if (rc != TWRC_SUCCESS) return rc; | |
// Save the image data to a file | |
FILE *file = fopen("scanned_image.bmp", "wb"); | |
if (file) { | |
fwrite(imageMemXfer.Memory.TheMem, 1, imageMemXfer.Memory.Length, file); | |
fclose(file); | |
} | |
return rc; | |
} | |
int main() { | |
TW_IDENTITY appId = {0}; | |
TW_IDENTITY sourceId = {0}; | |
TW_UINT16 rc; | |
// Initialize TWAIN | |
rc = InitializeTwain(&appId, &sourceId); | |
if (rc != TWRC_SUCCESS) { | |
printf("Failed to initialize TWAIN: %d\n", rc); | |
return 1; | |
} | |
// Acquire image | |
rc = AcquireImage(&appId, &sourceId); | |
if (rc != TWRC_SUCCESS) { | |
printf("Failed to acquire image: %d\n", rc); | |
return 1; | |
} | |
// Close the data source and DSM | |
DSM_Entry(&appId, NULL, DG_CONTROL, DAT_IDENTITY, MSG_CLOSEDS, &sourceId); | |
DSM_Entry(&appId, NULL, DG_CONTROL, DAT_PARENT, MSG_CLOSEDSM, (TW_MEMREF)NULL); | |
printf("Image acquired successfully.\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment