iOS Code Snippet Generate QR Codes

Discussion in 'iOS Code Snippets' started by JanPRO, Jul 6, 2015.

  1. JanPRO

    JanPRO Well-Known Member Licensed User

    Based on http://stackoverflow.com/questions/12051118/is-there-a-way-to-generate-qr-code-image-on-ios

    Code:
    Code:
    Dim NativeMe As NativeObject = Me
    NativeMe.RunMethod(
    "createQRForString::"Array ("Your text",ImageView1))


    #If OBJC
    - (void)createQRForString:(NSString *)qrString :(UIImageView *)qrImageView
    {
      // Need To convert the string To a UTF-8 encoded NSData object
      NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];

      // Create the filter
      CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
      // Set the message content And error-correction level
      [qrFilter setValue:stringData forKey:@"inputMessage"];
      [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
     
       // Remove blur effect with scaling
       CGAffineTransform transform = CGAffineTransformMakeScale(100.0f,100.0f);
       CIImage *qrOutput = [qrFilter.outputImage imageByApplyingTransform: transform];

       //Convert CIImage to UIImage
       UIImage *qrImage = [[UIImage alloc] initWithCIImage:qrOutput];
     
       //Set Image to ImageView
       [qrImageView setImage:qrImage];
    }
    #End If
     
    Humberto, f0raster0, Erel and 2 others like this.
  2. f0raster0

    f0raster0 Active Member Licensed User

    JanPro, I was looking for it..Thanks.

    I have attached a simple example :)
     

    Attached Files:

    Humberto, JanPRO and Douglas Farias like this.
  3. Humberto

    Humberto Active Member Licensed User

    I´m trying to save to a file the Qrcode generated , and send it by mail.

    Any other image I can save and send but the qrcode.

    I can copy the image to other imageview but when I save it to a file nothing is seeing as attachemnet in an email

    code to generate
    Code:
    NativeMe.RunMethod("createQRForString::"Array (b,Im_QrCode))
    Then to read the bitmap and save to a file and send

    Code:
    Dim Bm As Bitmap
        Bm = Im_QrCode.Bitmap
       
        
    Dim Out As OutputStream
        Out = 
    File.OpenOutput(File.DirTemp, "Im_QrCode.png"False)
        Bm.WriteToStream(Out, 
    100"PNG")
        Out.Close

    '    'Enviar email
        Dim xList As List
        xList.Initialize
        SendMail ( xList, 
    "QrCode gerado por QrCode HSP""A imagem em anexo foi gerada pelo programa QrCode HSP " ,File.DirTemp , "Im_QrCode.png""image/png" ) '"image/jpg" ) ' "image/png" )
    If I copy any other image to Im_QrCode.png the image is sent

    Code:
    File.Copy  (File.DirAssets, "money1.png"File.DirTemp , "Im_QrCode.png")
     
  4. JanPRO

    JanPRO Well-Known Member Licensed User

    I don't know why it doesn't work. However try this:

    Code:
    CreateQr("test",Null,File.DirDocuments,"test.png")
        
    Sub CreateQr(Text As String, IV As ImageView,Dir As String, PNGName As String)
        
    Dim NativeMe As NativeObject = Me
        
    Dim QRImage As Bitmap = NativeMe.RunMethod("createQRForString:"Array (Text))
       
        
    If IV.IsInitialized Then IV.Bitmap = QRImage
       
        
    Dim pn As Panel
        pn.Initialize(
    "")
        pn.Height = QRImage.Height
        pn.Width = QRImage.Width
       
        
    Dim Dest As Rect
        Dest.Initialize(
    0,0,QRImage.Width,QRImage.Height)
       
        
    Dim Ca As Canvas
        Ca.Initialize(pn)
        Ca.DrawBitmap(QRImage,Dest)
        Ca.Refresh
       
        
    Dim Out As OutputStream
        Out = 
    File.OpenOutput(Dir, PNGName,False)
        Ca.CreateBitmap.WriteToStream(Out, 
    100,"PNG")
        Out.Close
       
        
    #If OBJC
            - (UIImage *)createQRForString:(NSString *)qrString
            {
              // Need To convert the string To a UTF-8 encoded NSData object
              NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];

              // Create the filter
              CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
              // Set the message content And error-correction level
              [qrFilter setValue:stringData forKey:@"inputMessage"];
              [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
            
               // Remove blur effect with scaling
               CGAffineTransform transform = CGAffineTransformMakeScale(100.0f,100.0f);
               CIImage *qrOutput = [qrFilter.outputImage imageByApplyingTransform: transform];

               //Convert CIImage to UIImage
               UIImage *qrImage = [[UIImage alloc] initWithCIImage:qrOutput];
              
               return qrImage;
            }
        
    #End If
    End Sub
     
    ilan and Humberto like this.
  5. Humberto

    Humberto Active Member Licensed User

    Hi JanPro
    This works, but I´m sending the print screen of the image and not the original image.

    I Tryed to save the original bitmap to a file and doesn´t work either., instead of get the bitmap from the imageview
     
  6. JanPRO

    JanPRO Well-Known Member Licensed User

    Not sure what you mean by that. Can you explain it?

    Jan
     
  7. Humberto

    Humberto Active Member Licensed User

    The imageview is set to fill the image in and maybe is not the original size ( height and width ) so when I capture the printscreen it´s not the original picture.

    I don´t know why this bitmap can not be saved in a file. I can copy this bitmap to other imageview. Another picture loaded to an imageview can be saved to a file.

    In B4A works.

    How can I see the file in IPhone ?
     
  8. JanPRO

    JanPRO Well-Known Member Licensed User

    But the Bitmap property of the ImageView returns always the "original image".
     
  9. Humberto

    Humberto Active Member Licensed User

    Yes, but what I understand from your code is that you capture the screen image and save to a file.

    Is why I´m trying to get from imageview.bitmap
     
  10. JanPRO

    JanPRO Well-Known Member Licensed User

    Yes, and the result bitmap will be visual the same like the bitmap from the imageview, so I don't understand your problem.

    Jan
     
  11. Humberto

    Humberto Active Member Licensed User

    I don´t know if I´m wrong, but the result will depend of the size of the imageview in the panel, as I use "fill" property it´s adjust the image original.

    The orginalmimage is bigger that one displayed
     
  12. JanPRO

    JanPRO Well-Known Member Licensed User

    Yes, but only visual. A ImageView doesn't resize it's bitmap.
     
  13. Humberto

    Humberto Active Member Licensed User

    I don´t know if I´m wrong

    Your code draw a rect with the image inside, from now the originalmimage is gone and we have a rect with an image adjusted to that size.

    When we save it, is a different image.

    Right?
     
  14. JanPRO

    JanPRO Well-Known Member Licensed User

    Yes, but you can't see a difference.
     
    Humberto likes this.
Loading...