Page 3 of 3
Re: Scan2PDF
Posted: Wed Jan 04, 2017 12:30 pm
by coder14
harkon wrote:Code: Select all
Procedure.l hkTWAIN_WriteNativeToJPG(hDib.l, sJPGFileName.s, lQuality.l)
Define lpDib.l
Define StructSize.l
Define Width.l
Define Height.l
Define Planes.w
Define BitCount.w
Define Compression.l
Define SizeImage.l
Define XPix_Meter.l
Define YPix_Meter.l
Define ColorsUsed.l
Define ColorsImportant.l
Define PalletteSize.l
Define BMPOffset.l
Define BMPSize.l
Define ImageNumber.l
Define *BMPBuffer
lpDib=GlobalLock_(hDib)
StructSize=PeekL(lpDib)
Width=PeekL(lpDib+4)
Height=PeekL(lpDib+8)
Planes=PeekW(lpDib+12)
BitCount=PeekW(lpDib+14)
Compression=PeekL(lpDib+16)
SizeImage=PeekL(lpDib+20)
XPix_Meter=PeekL(lpDib+24)
YPix_Meter=PeekL(lpDib+28)
ColorsUsed=PeekL(lpDib+32)
ColorsImportant=PeekL(lpDib+36)
;make BMP for CatchImage
If BitCount<24
PalletteSize=BitCount*8
EndIf
;BMPFileHeader is
;"BM",BMPSize.l,Reserved.w, Reserved.w, Offset.l where bmp data starts
; the offset is after the pallette info
; pallette info is RGB+1 byte for each color in the pallette
; unless it's 24bit then there's no pallette
BMPOffset=14+StructSize+PalletteSize
BMPSize=BMPOffset+SizeImage
*BMPBuffer=AllocateMemory(BMPSize)
PokeS(*BMPBuffer,"BM")
PokeL(*BMPBuffer+2,BMPSize)
PokeL(*BMPBuffer+10,BMPOffset)
CopyMemory(lpDib, *BMPBuffer+14, BMPSize-14)
GlobalUnlock_(hDib)
ImageNumber=CatchImage(#PB_Any,*BMPBuffer,BMPSize)
If sJPGFileName=""
sJPGFileName.s=SaveFileRequester("Save JPG file",GetHomeDirectory()+"My Documents\","JPG (*.jpg)|*.jpg",0)
EndIf
;lQuality is spcified in terms of a number between 1 and 10, 10 being the best quality lowest compression
; if not specified and the quality is 0 or invalid, we will default to 7
If lQuality<1 Or lQuality>10
lQuality=7
EndIf
ProcedureReturn SaveImage(ImageNumber,sJPGFileName,#PB_ImagePlugin_JPEG, lQuality)
FreeImage(ImageNumber)
FreeMemory(*BMPBuffer)
EndProcedure
DisableExplicit
I tried this code but the image is converted "flipped"
The DIB is converted correctly with Intel's ijl.dll. Maybe some changes since 2010

Re: Scan2PDF
Posted: Wed Jan 04, 2017 3:54 pm
by harkon
Looks like a problem with the offset or bitcount. I ran into this before nut can't remember how I fixed it. Does this happen using all color options or just greycsale?
Re: Scan2PDF
Posted: Thu Jan 05, 2017 3:34 am
by coder14
harkon wrote:Looks like a problem with the offset or bitcount. I ran into this before nut can't remember how I fixed it. Does this happen using all color options or just greycsale?
Hi Harkon! Thanks for your quick reply. Yes! It's okay with pixel 4 but flips on pixel 2. I tried pixel 0, 1, 3 and get strange morphed images squeezed vertically and horizontally.

Re: Scan2PDF
Posted: Thu Jan 05, 2017 7:05 am
by Fangbeast
Harkon, i've got scanning going for different sections in my inventory program. One deals with scanning images for inventory products (and doesn't go to PDF as it isn't needed) and the other ia attaching documents by scanning to PDF.
This one is a keeper and I thank you heaps for it.
Re: Scan2PDF
Posted: Thu Jan 05, 2017 10:25 am
by coder14
@harkon: Can we use this function to convert the twain DIB to bitmap then save it?
http://www.purebasic.fr/english/viewtop ... 13&t=67403
Re: Scan2PDF
Posted: Thu Jan 05, 2017 4:11 pm
by harkon
I don't see why not. This could prove a really good and more robust solution. I don't have time to flesh this out and rework my app, but I did find some VB code to do this. It's on my dropbox;
https://dl.dropboxusercontent.com/u/653 ... lasses.zip
If you get this working, please post back.
Re: Scan2PDF
Posted: Thu Jan 05, 2017 4:23 pm
by harkon
coder14 wrote:harkon wrote:Code: Select all
Procedure.l hkTWAIN_WriteNativeToJPG(hDib.l, sJPGFileName.s, lQuality.l)
Define lpDib.l
Define StructSize.l
Define Width.l
Define Height.l
Define Planes.w
Define BitCount.w
Define Compression.l
Define SizeImage.l
Define XPix_Meter.l
Define YPix_Meter.l
Define ColorsUsed.l
Define ColorsImportant.l
Define PalletteSize.l
Define BMPOffset.l
Define BMPSize.l
Define ImageNumber.l
Define *BMPBuffer
lpDib=GlobalLock_(hDib)
StructSize=PeekL(lpDib)
Width=PeekL(lpDib+4)
Height=PeekL(lpDib+8)
Planes=PeekW(lpDib+12)
BitCount=PeekW(lpDib+14)
Compression=PeekL(lpDib+16)
SizeImage=PeekL(lpDib+20)
XPix_Meter=PeekL(lpDib+24)
YPix_Meter=PeekL(lpDib+28)
ColorsUsed=PeekL(lpDib+32)
ColorsImportant=PeekL(lpDib+36)
;make BMP for CatchImage
If BitCount<24
PalletteSize=BitCount*8
EndIf
;BMPFileHeader is
;"BM",BMPSize.l,Reserved.w, Reserved.w, Offset.l where bmp data starts
; the offset is after the pallette info
; pallette info is RGB+1 byte for each color in the pallette
; unless it's 24bit then there's no pallette
BMPOffset=14+StructSize+PalletteSize
BMPSize=BMPOffset+SizeImage
*BMPBuffer=AllocateMemory(BMPSize)
PokeS(*BMPBuffer,"BM")
PokeL(*BMPBuffer+2,BMPSize)
PokeL(*BMPBuffer+10,BMPOffset)
CopyMemory(lpDib, *BMPBuffer+14, BMPSize-14)
GlobalUnlock_(hDib)
ImageNumber=CatchImage(#PB_Any,*BMPBuffer,BMPSize)
If sJPGFileName=""
sJPGFileName.s=SaveFileRequester("Save JPG file",GetHomeDirectory()+"My Documents\","JPG (*.jpg)|*.jpg",0)
EndIf
;lQuality is spcified in terms of a number between 1 and 10, 10 being the best quality lowest compression
; if not specified and the quality is 0 or invalid, we will default to 7
If lQuality<1 Or lQuality>10
lQuality=7
EndIf
ProcedureReturn SaveImage(ImageNumber,sJPGFileName,#PB_ImagePlugin_JPEG, lQuality)
FreeImage(ImageNumber)
FreeMemory(*BMPBuffer)
EndProcedure
DisableExplicit
I tried this code but the image is converted "flipped"
The DIB is converted correctly with Intel's ijl.dll. Maybe some changes since 2010

Coder, I just remembered something. Did you by chance leave the unicode option checked in the compiler options. I can't remember for sure, but I think this will mess with peeks and pokes. Try as a non-unicode app and see if this helps.
Re: Scan2PDF
Posted: Thu Jan 05, 2017 4:26 pm
by coder14
harkon wrote:
I don't see why not. This could prove a really good and more robust solution. I don't have time to flesh this out and rework my app, but I did find some VB code to do this. It's on my dropbox;
https://dl.dropboxusercontent.com/u/653 ... lasses.zip
If you get this working, please post back.
Thank you. I can't find that function in your classes but I'll study them more.

Re: Scan2PDF
Posted: Sun Jan 08, 2017 5:45 am
by coder14
harkon wrote:Coder, I just remembered something. Did you by chance leave the unicode option checked in the compiler options. I can't remember for sure, but I think this will mess with peeks and pokes. Try as a non-unicode app and see if this helps.
Hello harkon. For EZTW32.dll to work I need to use the x86 version of PB which is ASCII default. But TI-994A modified your code and it works now - no more flipping but the RGB pixel type is still 4 and not 3. It is very similar to your original code. I am still trying to figure out the differences.
http://www.purebasic.fr/english/viewtop ... 27#p500027
Thank you for your help. Many great gurus helped to solve this one.

Re: Scan2PDF
Posted: Sat Apr 03, 2021 8:29 am
by Fangbeast
Harkon, I have 2 scanners connected and this code only finds the primary one that is currently disconnected as it is only used once every 6 months (maybe more) and of course I can go no further.
Is there any way to pop a dialogue to select the scanner that you want please? I can't do it as it's API that I don't understand.
The secondary scanner is the one always in use but it was installed 5 years after the primary and I need them both.