iOS Question Save iBarcode preview as a bitmap?

JackKirk

Well-Known Member
Licensed User
Longtime User
Is it possible to save the iBarcode preview as a bitmap?

I have tried code of the form:
B4X:
Private temp_imageview As ImageView
Private temp_rect As Rect
Private temp_bmp As Bitmap
Private temp_canvas As Canvas

temp_imageview.Initialize("")

Page1.RootPanel.AddView(temp_imageview, 0, 0, Page1.RootPanel.Width, Page1.RootPanel.Height)
temp_imageview.Visible = False

temp_canvas.Initialize(temp_imageview)

temp_rect.Initialize(0, 0, Page1.RootPanel.Width, Page1.RootPanel.Height)

temp_canvas.DrawView(scanner_panel, temp_rect)

temp_canvas.Refresh

temp_bmp = BC_Canvas.CreateBitmap

Dim temp_phone As Phone
temp_phone.AddImageToAlbum(temp_bmp)
where scanner_panel is the panel I have initialized the barcode scanner instance to.

I get a photo which shows all the other views I have dumped into the scanner_panel but no barcode preview.

Thanks in anticipation...
 
Last edited:

JackKirk

Well-Known Member
Licensed User
Longtime User
Jan,

OK, I have a prototype working, the guts of which looks something like this:
B4X:
Private Sub Event_Timer_Tick

    If Not(BC_Capturing_StillImage) Then

        BC_Capturing_StillImage = True

        BC_Scanner.CaptureStillImage
   
    End If

End Sub

Sub Event_BC_Scanner_StillImage(Image As Bitmap, Error As Exception)

    If Not(Error) Then
   
'Dim xxxmsg As String
'Dim start_dt As Long
'start_dt = DateTime.Now
    
        Dim no As NativeObject = Me
        Dim bmp_bytes() As Byte = no.NSDataToArray(no.RunMethod("BitmapToArray:", Array(Image)))

'xxxmsg = "Stage 1 " & (DateTime.Now - start_dt)  & " millisecs"
'start_dt = DateTime.Now

        Dim iii As Long
        Dim xxx, yyy, stepsize As Int
        Dim bright As Double
       
        stepsize = 64
        bright = 0

        For xxx = (stepsize / 2 - 1) To Image.Width - 1 Step stepsize

            For yyy = (stepsize / 2 - 1) To Image.Height - 1 Step stepsize

                iii = (xxx + yyy * Image.Width) * 4
                'This formula comes from
                'https://en.wikipedia.org/wiki/YUV
                bright = bright + .299 * bmp_bytes(iii) + .587 * bmp_bytes(iii + 1) + .114 * bmp_bytes(iii + 2)

            Next
   
        Next

        bright = bright / Ceil((Image.Width - stepsize / 2) / stepsize) / Ceil((Image.Height - stepsize / 2) / stepsize)

'xxxmsg = xxxmsg & CRLF & "Stage 2 " & (DateTime.Now - start_dt)  & " millisecs"
'xxxmsg = xxxmsg & CRLF & "brightness=" & NumberFormat2(bright, 1, 0, 0, False)
'Msgbox(xxxmsg,"")    

    Else
   
        Log(Error.Description)
   
    End If

    BC_Capturing_StillImage = False

End Sub

#If OBJC
- (NSData*)BitmapToArray:(UIImage*) bmp {
   CGDataProviderRef provider = CGImageGetDataProvider(bmp.CGImage);
   NSData* data = (id)CFBridgingRelease(CGDataProviderCopyData(provider));
   return data;
}
#End If

Essentially what I am doing is reducing the RGBA of the bitmap to Y of YUV, Y effectively being the brightness.

When I uncomment the xxxmsg and start_dt stuff and build a release app it tells me that the vast majority of the time (about 350 msec on iPhone 4S) is spent in these 2 lines:

Dim no As NativeObject = Me
Dim bmp_bytes() As Byte = no.NSDataToArray(no.RunMethod("BitmapToArray:", Array(Image)))


Over in B4A, Erel's CameraEx class has a _Preview event that receives the preview as YUV in a byte array.

Would it be possible to do a similar thing here? - then there would be no need for any conversions.

I have done a little googling and come up with:

http://stackoverflow.com/questions/...deo-from-the-camera-display-it-and-process-it

which seems to suggest it is quite simple (for someone who has Objective C skills).

Also, every call to the CaptureStillImage method generates a pseudo camera shutter sound - is there any way to suppress this.

Thanks again for your help...
 
Last edited:
Upvote 0

JanPRO

Well-Known Member
Licensed User
Longtime User
Also, every call to the CaptureStillImage method generates a pseudo camera shutter sound - is there any way to suppress this.
Officially it's not possible. However it exists a kind of hack.

Would it be possible to do a similar thing here?
At first glance it seems complicated to add such a feature to the library, but when I have more time I will have a closer look at the sources.

Jan
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Officially it's not possible. However it exists a kind of hack.
Jan,

Some fascinating workarounds there - all over my head unfortunately.

One of the posts in that thread is entitled:

You can also take a frame from a video stream to capture a (not full resolution) image.

and references:

https://github.com/CyberAgent/iOS-N...5af3b8273a/Source/Camera/NBUCameraView.m#L699

which looks like it takes frames from the video stream.

For my ultimate objective - which is to get an average "brightness" of the frame - I suspect it will not matter very much if the image is not full resolution.

So maybe the solution to both avoiding pseudo camera shutter sound and delivering YUV is to just capture video frames?
but when I have more time I will have a closer look at the sources.

I would really appreciate this.

Thanks in anticipation...
 
Upvote 0

narek adonts

Well-Known Member
Licensed User
Longtime User
U can use LLcamera_
Jan,

Some fascinating workarounds there - all over my head unfortunately.

One of the posts in that thread is entitled:

You can also take a frame from a video stream to capture a (not full resolution) image.

and references:

https://github.com/CyberAgent/iOS-N...5af3b8273a/Source/Camera/NBUCameraView.m#L699

which looks like it takes frames from the video stream.

For my ultimate objective - which is to get an average "brightness" of the frame - I suspect it will not matter very much if the image is not full resolution.

So maybe the solution to both avoiding pseudo camera shutter sound and delivering YUV is to just capture video frames?


I would really appreciate this.

Thanks in anticipation...


You can try this

B4X:
Dim ll as LLCamera

ll.Initialize(myPanel,"myCamera",True)
ll.StartPreview

Sub myCamera_Preview (Image As Bitmap)
   
    Dim myCapturedImage=Image
    ll.ReleaseFrame
    ll.StopPreview
   
End Sub

So you will have the preview image and you can process it without sound.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Narek,

Thanks for your interest.

Yes, I have been all over LLCamera - trouble is I can't run it at same time as barcode scanner - I'm after a real time brightness feed while barcode scanning - as Johan Schoeman has achieved in B4A:

https://www.b4x.com/android/forum/t...-100-embedded-in-b4a.63764/page-2#post-410694

and I have subsequently wrapped at:

https://www.b4x.com/android/forum/t...full-correct-torch-control.64931/#post-411092

If you read the second post fully you will get a feel for what I am trying to do in B4I.

Regards...
 
Upvote 0

narek adonts

Well-Known Member
Licensed User
Longtime User
Narek,

You are an Objective C guy, what about having a go at:

https://www.b4x.com/android/forum/threads/disable-the-voice-of-take-picture.54825/#post-411915

Regards...
B4X:
Sub Mute
Dim no as NativeObject=Me
No.runMethod("mute", array(null))
End Sub

#If OBJC

-(void) mute {
MPVolumeView* volumeView = [[MPVolumeView alloc] init];
//find the volumeSlider
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
    if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
        volumeViewSlider = (UISlider*)view;
        break;
    }
}
// mute it here:
[volumeViewSlider setValue:0.0f animated:YES];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
}

#End If
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Narek,

I put your code from previous post in a new B4I project (see attached zip) and got a smash of protests in the compile log:

B4X:
B4i version: 2.50
Parsing code.  (0.00s)
Compiling code.  (0.01s)
Compiling layouts code.  (0.00s)
Compiling debugger engine code.  (0.81s)
Building Xcode project  (0.01s)
Sending data to remote compiler.  Error
B4i line: 19
End Sub
use of undeclared identifier 'MPVolumeView'
Out: Build settings from command line:
  ARCHS = armv7
  CODE_SIGN_IDENTITY = iPhone
  CONFIGURATION_BUILD_DIR = /Users/administrator/Documents/UploadedProjects/<user id>/Payload
  OTHER_CODE_SIGN_FLAGS = --keychain <user id>
  PRODUCT_NAME = B4i Example
  PROVISIONING_PROFILE = bf4287f9-efcd-4c12-a3e9-287c3bd094b5
=== BUILD TARGET B4iProject OF PROJECT B4iProject WITH CONFIGURATION Release ===
Check dependencies
Write auxiliary files
write-file /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-project-headers.hmap
/bin/mkdir -p /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7
write-file /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/B4i\ Example.LinkFileList
write-file /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example.hmap
write-file /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-all-non-framework-target-headers.hmap
write-file /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-own-target-headers.hmap
write-file /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-all-target-headers.hmap
write-file /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-generated-files.hmap
Create product structure
/bin/mkdir -p /Users/administrator/Documents/UploadedProjects/<user id>/Payload/B4i\ Example.app
ProcessInfoPlistFile Payload/B4i\ Example.app/Info.plist B4iProject/B4iProject-Info.plist
  cd /Users/administrator/Documents/UploadedProjects/<user id>
  export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
  builtin-infoPlistUtility /Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/B4iProject-Info.plist -genpkginfo /Users/administrator/Documents/UploadedProjects/<user id>/Payload/B4i\ Example.app/PkgInfo -expandbuildsettings -format binary -platform iphoneos -o /Users/administrator/Documents/UploadedProjects/<user id>/Payload/B4i\ Example.app/Info.plist
CompileC build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/b4i_main.o B4iProject/b4i_main.m normal armv7 objective-c com.apple.compilers.llvm.clang.1_0.compiler
  cd /Users/administrator/Documents/UploadedProjects/<user id>
  export LANG=en_US.US-ASCII
  export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch armv7 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu99 -fobjc-arc -fmodules -gmodules -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/var/folders/lj/wrbzzxds6b53f88_rx3x7c800000gn/C/org.llvm.clang/ModuleCache/Session.modulevalidation -fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wno-implicit-atomic-properties -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-arc-repeated-use-of-weak -Wduplicate-method-match -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wundeclared-selector -Wno-deprecated-implementations -DNS_BLOCK_ASSERTIONS=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.1.sdk -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -miphoneos-version-min=7.0 -fvisibility=hidden -Wno-sign-conversion -iquote /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-generated-files.hmap -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-own-target-headers.hmap -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-all-target-headers.hmap -iquote /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-project-headers.hmap -I/Users/administrator/Documents/UploadedProjects/<user id>/Payload/include -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/DerivedSources/armv7 -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/DerivedSources -F/Users/administrator/Documents/UploadedProjects/<user id>/Payload -F../../Libs -MMD -MT dependencies -MF /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/b4i_main.d --serialize-diagnostics /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/b4i_main.dia -c /Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/b4i_main.m -o /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/b4i_main.o
/Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/b4i_main.m:114:1: error: use of undeclared identifier 'MPVolumeView'
MPVolumeView* volumeView = [[MPVolumeView alloc] init];
^
/Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/b4i_main.m:114:15: error: use of undeclared identifier 'volumeView'
MPVolumeView* volumeView = [[MPVolumeView alloc] init];
  ^
/Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/b4i_main.m:114:30: error: use of undeclared identifier 'MPVolumeView'
MPVolumeView* volumeView = [[MPVolumeView alloc] init];
  ^
/Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/b4i_main.m:117:23: error: use of undeclared identifier 'volumeView'
for (UIView *view in [volumeView subviews]){
  ^
4 errors generated.
CompileC build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/main.o B4iProject/main.m normal armv7 objective-c com.apple.compilers.llvm.clang.1_0.compiler
  cd /Users/administrator/Documents/UploadedProjects/<user id>
  export LANG=en_US.US-ASCII
  export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch armv7 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu99 -fobjc-arc -fmodules -gmodules -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/var/folders/lj/wrbzzxds6b53f88_rx3x7c800000gn/C/org.llvm.clang/ModuleCache/Session.modulevalidation -fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wno-implicit-atomic-properties -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-arc-repeated-use-of-weak -Wduplicate-method-match -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wshorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wundeclared-selector -Wno-deprecated-implementations -DNS_BLOCK_ASSERTIONS=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.1.sdk -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -miphoneos-version-min=7.0 -fvisibility=hidden -Wno-sign-conversion -iquote /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-generated-files.hmap -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-own-target-headers.hmap -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-all-target-headers.hmap -iquote /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/B4i\ Example-project-headers.hmap -I/Users/administrator/Documents/UploadedProjects/<user id>/Payload/include -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/DerivedSources/armv7 -I/Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/DerivedSources -F/Users/administrator/Documents/UploadedProjects/<user id>/Payload -F../../Libs -MMD -MT dependencies -MF /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/main.d --serialize-diagnostics /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/main.dia -c /Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/main.m -o /Users/administrator/Documents/UploadedProjects/<user id>/build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/main.o

Error: ** BUILD FAILED **

The following build commands failed:
 CompileC build/B4iProject.build/Release-iphoneos/B4iProject.build/Objects-normal/armv7/b4i_main.o B4iProject/b4i_main.m normal armv7 objective-c com.apple.compilers.llvm.clang.1_0.compiler
(1 failure)

Any thoughts?

Regards...
 

Attachments

  • Test.zip
    1.1 KB · Views: 304
Upvote 0
Top