iOS Code Snippet Generate QR Codes

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

Code:
B4X:
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

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
B4X:
    NativeMe.RunMethod("createQRForString::", Array (b,Im_QrCode))

Then to read the bitmap and save to a file and send

B4X:
    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

B4X:
File.Copy  (File.DirAssets, "money1.png", File.DirTemp , "Im_QrCode.png")
 

JanPRO

Well-Known Member
Licensed User
I don't know why it doesn't work. However try this:

B4X:
    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
 

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
 

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 ?
 

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
 

JanPRO

Well-Known Member
Licensed User
Yes, but what I understand from your code is that you capture the screen image and save to a file.
Yes, and the result bitmap will be visual the same like the bitmap from the imageview, so I don't understand your problem.

Jan
 

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
 

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?
 
Top