0

I am trying to generate a qr and export it to PNG but, whenever I do that, it exports it as a blurred image. I am not sure what I can do to make it not blurry.

It happens in 2 cases:

  1. in the preview window
  2. when I export a file

Here is the code I am using. Any help or advise would be greatly appreciated as I am still new to the language and trying to figure stuff out.

Here comes my code:

- (NSImage *)scaleImageTo:(NSImage *)inputImage size:(NSSize)newSize
{
    NSImage *cropped = [[NSImage alloc] initWithSize:NSMakeSize(newSize.width, newSize.height)];

     [cropped lockFocus];
     [inputImage drawInRect:NSMakeRect(0,0,cropped.size.width,cropped.size.height)
                fromRect:NSMakeRect(0,0,inputImage.size.width,inputImage.size.height)
               operation:NSCompositeCopy
                fraction:1.0];
     [cropped unlockFocus];


     NSRect rect = NSMakeRect(0, 0, cropped.size.width, cropped.size.height);

     CIImage* ciImage1 = [[CIImage alloc] initWithData:[cropped TIFFRepresentation]];


    [[NSGraphicsContext currentContext] setShouldAntialias: NO];

     NSImage *buffer2 = [[NSImage alloc] initWithSize:NSMakeSize([cropped size].width , [cropped size].height)];

    [buffer2 lockFocus];
    CIFilter* filter2 = [CIFilter filterWithName:@"CIMaskToAlpha"];
    [filter2 setValue:ciImage1 forKey:@"inputImage"];

    CIImage* output2 = [filter2 valueForKey:@"outputImage"];

    [output2 drawAtPoint:NSZeroPoint fromRect:rect operation:NSCompositeSourceOver fraction:1.0];


    //background
    [[_backgroundColorSelector color] set];
    NSRectFillUsingOperation(rect, NSCompositeSourceAtop);
    //foreground
    [[_foregroundColorSelector color] set];
    NSRectFillUsingOperation(rect, NSCompositeDestinationAtop);

    [buffer2 unlockFocus];
    return buffer2;
}
- (void)writeImageAt:(NSImage *)img path:(NSString *)strPath
{
    //create a NSBitmapImageRep
     NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]initWithData:[img TIFFRepresentation]];
     //add the NSBitmapImage to the representation list of the target
     [img addRepresentation:bmpImageRep];

     //get the data from the representation
     NSData *data = [bmpImageRep representationUsingType: NSPNGFileType
                                             properties: nil];
    [data writeToFile:strPath atomically:YES];
}
- (void)savePNGAt:(NSImage *)img path:(NSString *)strPath
{
    NSString * qrWidth = [_qrWidth stringValue];
    NSString * qrHeight = [_qrHeight stringValue];

    if (qrHeight != nil && ![qrWidth isEqualToString:@""] && qrWidth != nil && ![qrHeight isEqualToString:@""]) {
        NSSize newSize = NSMakeSize([qrWidth floatValue], [qrHeight floatValue]);
        img = [self scaleImageTo:img size:newSize];
        [self writeImageAt:img path:strPath];
    }
    else
    {
        img = [self changeImageColor:img];
        [self writeImageAt:img path:strPath];
    }
}
Krizz
  • 11,362
  • 1
  • 30
  • 43
incoe
  • 112
  • 1
  • 2
  • 12

1 Answers1

0

almost certainly the blurriness is introduced when the image is scaled img = [self scaleImageTo:img size:newSize]; rather than doing that you should reproduce the QR image in the correct size to be displayed/printed/exported/etc.

gordy
  • 9,360
  • 1
  • 31
  • 43