3

I'm making an application to easily scan to multiple page pdf files. The project is on GitHub, just in case you want to have a look at all of the project code.

I'm having an issue with scanning in black & white.

This is the method that gets called when I press the button to start scanning.

- (IBAction)scan:(id)sender {
    //Get the selected scanner and it's functional unit
    ICScannerDevice *scanner = [self selectedScanner];
    ICScannerFunctionalUnit *unit = [scanner selectedFunctionalUnit];

    //If there is no scan or overviewscan in progress
    if (![unit overviewScanInProgress] && ![unit scanInProgress]) {
        //Setup the functional unit and start the scan
        [unit setScanArea:[self scanArea]];
        [unit setResolution:[[unit supportedResolutions] indexGreaterThanOrEqualToIndex:[[resolutionPopUpButton selectedItem] tag]]];
        [unit setBitDepth:ICScannerBitDepth8Bits];
        [unit setMeasurementUnit:ICScannerMeasurementUnitCentimeters];
        [unit setThresholdForBlackAndWhiteScanning:0];
        [unit setUsesThresholdForBlackAndWhiteScanning:YES];
        [unit setPixelDataType:[kindSegmentedControl selectedSegment]];
        [scanner requestScan];
    } else {
        //Cancel the ongoing scan
        [scanner cancelScan];
    }
}

I'm setting the pixelDataType to an integer that I get from an NSSegmentedControl. When the first segment is selected this will return 0, which is the same as ICScannerPixelDataTypeBW.

However, despite everything working fine when the second and the third segment are selected (which are ICScannerPixelDataTypeGray and ICScannerPixelDataTypeRGB), the scanner does nothing when set to scan black & white.

There is very little documentation available on the scanning part of ImageCaptureCore, but I found those properties describing a threshold for black & white scanning on this website, but none of them worked for me.

I know this is a part of the ImageCaptureCore API that doesn't get used by many people very often, but I really hope someone knows, or at least can find out, a solution to my problem.

Edit:

I added - (void)device:(ICDevice *)device didEncounterError:(NSError *)error to my implementation and logged the error, which is:

2014-02-01 21:55:16.260 Scanner[4131:903] Error Domain=com.apple.ImageCaptureCore Code=-9933 UserInfo=0x1005763f0 "An error occurred during scanning."
levidhuyvetter
  • 401
  • 5
  • 16
  • Hi, I am also trying to perform headless scanning using ImageCaptureCore framework akin to what you are describing here. The problem I am facing is that I am not getting any callbacks after the `requestScan` call. I have asked a question on SO regarding this: http://stackoverflow.com/questions/35434605/imagecapturecore-functionality-not-getting-callbacks-despite-the-delegates-bein , but to no avail. Could you please see what I may be missing? – Sankalp Feb 17 '16 at 05:37

2 Answers2

4

With a little hint (intentional or not) from the other answer, I figured things out by myself.

What you have to do is to set bitDepth to ICScannerBitDepth1Bit, as what you're trying to scan is a 1 bit per pixel image.

This in turn disables scanning in grayscale or rgb.

Since I can't award bounties to my own questions, I will award the bounty to the question I got the hint from.

levidhuyvetter
  • 401
  • 5
  • 16
2

You can find the ImageCaptureCore error codes in ICCommonConstants.h.

The error you mention is

ICReturnReceivedUnsolicitedScannerErrorInfo   = -9933

Maybe your scanner simply does not support scanning in Black & White? Since Grayscale works, do you even want to use BW?

From the headers:

/*!
  @enum ICScannerPixelDataType
  @abstract Pixel data types. Corresponds to "ICAP_PIXELTYPE" of the TWAIN Specification.
  @constant ICScannerPixelDataTypeBW Monochrome 1 bit pixel image.
  @constant ICScannerPixelDataTypeGray 8 bit pixel Gray color space.
  @constant ICScannerPixelDataTypeRGB Color image RGB color space.
  @constant ICScannerPixelDataTypePalette Indexed Color image.
  @constant ICScannerPixelDataTypeCMY Color image in CMY color space.
  @constant ICScannerPixelDataTypeCMYK Color image in CMYK color space.
  @constant ICScannerPixelDataTypeYUV Color image in YUV color space.
  @constant ICScannerPixelDataTypeYUVK Color image in YUVK color space.
  @constant ICScannerPixelDataTypeCIEXYZ Color image in CIEXYZ color space.
*/

enum
{
    ICScannerPixelDataTypeBW      = 0,
    ICScannerPixelDataTypeGray    = 1,
    ICScannerPixelDataTypeRGB     = 2,
    ICScannerPixelDataTypePalette = 3,
    ICScannerPixelDataTypeCMY     = 4,
    ICScannerPixelDataTypeCMYK    = 5,
    ICScannerPixelDataTypeYUV     = 6,
    ICScannerPixelDataTypeYUVK    = 7,
    ICScannerPixelDataTypeCIEXYZ  = 8
};
typedef NSUInteger ICScannerPixelDataType;

Do you really want to support a 1bit per pixel image?

pfandrade
  • 2,359
  • 15
  • 25
  • Yeah I do want to support black & white. The application is designed for scanning text documents. I personally like to scan text documents in black & white to keep the file size low, and the quality isn't really important in that case. Is there a way I could achieve the same result by converting the grayscale image? CoreImage perhaps? What would be the best way to do it with CoreImage? – levidhuyvetter Feb 05 '14 at 23:13