iOS Question [SOLVED] Equivalent of B4A Phone.PhoneSensors(TYPE_LIGHT)

JackKirk

Well-Known Member
Licensed User
Longtime User
I have just recently completed my first B4A app and am now looking at porting it to B4I.

My B4A app uses the ABZxing library which launches the ZXing barcode scanner.

One of the settings in the ZXing barcode scanner is "Front Light/On/Automatic/Off".

The apps are likely to be used in indifferent ambient light conditions - hence use of the Front Light/Automatic option is highly recommended.

In B4I I can see I can use the iBarcode library for barcode scanning and iPhone.Phone.SetFlashLight to turn the front light on and off.

To fully imitate the ZXing Front Light/Automatic feature I need the ability to detect the ambient light condition in B4I - the equivalent of B4A Phone.PhoneSensors(TYPE_LIGHT).

This capability doesn't seem to exist - your suggestions are eagerly sought.

BTW a bit of googling came up with this site:

http://iphonedevwiki.net/index.php/AppleISL29003

which seems to be talking about the sort of capability desired - but way over my head.

Thanks in anticipation...
 

JackKirk

Well-Known Member
Licensed User
Longtime User
Digging a bit deeper, the introduction to the iBarcode library written by Erel:

https://www.b4x.com/android/forum/threads/ibarcode-library.47354/

indicates it is based on the open source project:

https://github.com/mikebuss/MTBBarcodeScanner

looking through this open source project you find:

-----------------
Controlling the Torch
To control the torch, set the torchMode property or call the toggleTorch method.
Available values include:
MTBTorchModeOff,
MTBTorchModeOn,
MTBTorchModeAuto
-----------------

so I guess my question/s become:

(1) does MTBTorchModeAuto mean the torch is automatically turned on if ambient light conditions dictate?
(2) is control of the torch already embedded in the iBarcode library and just set permanently to MTBTorchModeAuto (I wish)?
(3) failing (2) how can I flick the torch to MTBTorchModeAuto?

Thanks again...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

Thanks, I think I can follow what I need to do.

I am still in early investigation stage - waiting on an iPhone to arrive - so probably a couple of weeks until I actually get to this.

Is it reasonable to expect a torch feature to appear in the iBarcode library at some stage?

Regards...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

It is obvious that B4I is a lot less mature product than B4A and therefor is still in need of significant polishing.

A significant part of my app/s functionality revolves around barcode scanning and as such I can assure you an automatic torch capability is essential.

I can quite happily tackle it as above but would encourage you to apply this bit of polish sooner rather than later.

Regards...

BTW I have to say I am hugely impressed so far with your products - if they are (as I suspect) largely a one man effort then I am astounded.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

I tried your suggestion at post #3 by incorporating it into the Barcode example you supplied at:

https://www.b4x.com/android/forum/threads/ibarcode-library.47354/#content

I can get a manual torch on/off to work but not an auto torch.

The answer at this link:

http://stackoverflow.com/questions/...to-doesnt-seem-to-activate-torch-in-low-light

says "the auto torch mode doesn't work unless the AVCaptureSession has a video output".

I am assuming that, best case, your Objective C needs bulking up - or - worst case, its not doable.

As the project on which you based the iBarcode library seems to have the auto torch capability I'm hoping it is best case.

I would appreciate your response...

BTW turning the torch on seems to vastly improve speed of barcode reading.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

I'm not sure that forcing the torch on in bright light conditions will get better results.

At the risk of really annoying you, the link you gave in the previous post has some Objective C:

B4X:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
      didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
             fromConnection:(AVCaptureConnection *)connection
{
  CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL,
    sampleBuffer, kCMAttachmentMode_ShouldPropagate);
  NSDictionary *metadata = [[NSMutableDictionary alloc]
    initWithDictionary:(__bridge NSDictionary*)metadataDict];
  CFRelease(metadataDict);
  NSDictionary *exifMetadata = [[metadata
    objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
  float brightnessValue = [[exifMetadata
    objectForKey:(NSString *)kCGImagePropertyExifBrightnessValue] floatValue];
}


Which is entitled "Getting brightness value to decide enabling of Torch or not".

I experimented with trying to wrap it to get the value for the variable "brightnessValue" out to B4i but got nowhere.

Would it be possible to get you to wrap it?

Thanks again...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It will not work as it will interfere with the library.

When I wrapped this library the torch mode property was not available. It was added in a later version.

Looking over the code it is clear that auto is not really supported:
B4X:
- (void)toggleTorch {
if (self.torchMode == MTBTorchModeAuto || self.torchMode == MTBTorchModeOff) {
self.torchMode = MTBTorchModeOn;
} else {
self.torchMode = MTBTorchModeOff;
}
}
As you can see it just sets it to On.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

Once again thank you for your speedy response.

A couple of questions/comments:

(1) TorchMode = TORCH_ON and TORCH_OFF work fine.
(2) If I try TorchMode = TORCH_AUTO the torch never turns on, even in total darkness.
(3) I'm using an iPhone 4s - I'm wondering if it has the ability to support TORCH_AUTO - the phones camera and video recorder both have Auto flash settings which work - any suggestions on how to test for this?
(4) The new AutoTapToFocus feature - my experience is the camera automatically focuses (which I think is a feature you added a while back) - what is the value/point of this new feature?

Thanks again...
 
Last edited:
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Erel,

Mike Buss, developer of the GitHub project, has responded to the issue I raised and you can view it here:

https://github.com/mikebuss/MTBBarcodeScanner/issues/64

Looks like this is pretty much a dead end.

Interestingly, he refers to the link you gave in post #8 as a possible way forward:

http://stackoverflow.com/questions/...tinuously-update-torch-mode/28298472#28298472

I would like to explore this but need help with wrapping the Objective C - any chance of revisiting my request at post #9?

I understand you may be too busy in which case do you know of anyone else in the community I could direct this request to?

Thanks again...
 
Last edited:
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Can't you add a torch on / off button and let the user manually control the flash?

I can and have - however the whole point of the exercise is to make the scanning as simple as possible for the user - who typically has very limited technical abilities - he/she will have enough trouble just positioning the camera over the barcode so that it focuses.

Anyway, the library source code is attached if you or anyone else want to update it.

Not within my skill set I'm afraid (the main reason I'm using B4A/B4I).

I'm not certain that updating the iBarcode library is even appropriate or necessary - a separate preliminary reading of brightness (by something like the code at post #9) to set the initial torch state supplemented by an on/off user toggle would probably suffice.

Anyrate it looks like I have pushed this as far as possible.

Thanks for your patience.
 
Upvote 0
Top