Sunday, 26 August 2018

Generate QR Code for a string [Swift 4]


func generateQRcode(from string: String) -> UIImage? {
     
        let data = string.data(using: String.Encoding.ascii)
     
        if let filter = CIFilter(name: "CIQRCodeGenerator") {
            filter.setDefaults()

            filter.setValue(data, forKey: "inputMessage")

            let transform = CGAffineTransform(scaleX: 3, y: 3)
         
            if let output = filter.outputImage?.transformed(by: transform) {
                let context:CIContext = CIContext.init(options: nil)
                let cgImage:CGImage = context.createCGImage(output, from: output.extent)!
                let rawImage:UIImage = UIImage.init(cgImage: cgImage)
             

                let cgimage: CGImage = (rawImage.cgImage)!
                let cropZone = CGRect(x: 0, y: 0, width: Int(rawImage.size.width), height: Int(rawImage.size.height))
                let cWidth: size_t  = size_t(cropZone.size.width)
                let cHeight: size_t  = size_t(cropZone.size.height)
                let bitsPerComponent: size_t = cgimage.bitsPerComponent
             
                let bytesPerRow = (cgimage.bytesPerRow) / (cgimage.width  * cWidth)
             
                let context2: CGContext = CGContext(data: nil, width: cWidth, height: cHeight, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: cgimage.bitmapInfo.rawValue)!
             
                context2.draw(cgimage, in: cropZone)
             
                let result: CGImage  = context2.makeImage()!
                let finalImage = UIImage(cgImage: result)
             
                return finalImage
             
            }
        }
     
        return nil
    }


Usage:

// It will return an image

let image = generateBarcode(from: "Type_Your_String_Here")



Friday, 16 September 2016

Java program to Merge the Splited File

import java.io.*;
class Merger
{
public static void main(String[] args) throws IOException
{
Console con=System.console();
System.out.print("Enter destination file name : ");
String fname=con.readLine();
File f=new File(fname);
if (f.exists())
f.delete();

FileOutputStream fos=new FileOutputStream(fname);
int i=0;
File f1=null;
while (true)
{
f1=new File(""+i+f);
if (!f1.exists())
{
break;
}
FileInputStream fis=new FileInputStream(f1);
int ch;
while((ch=fis.read())!=-1)
fos.write((char)ch);
fis.close();
f1.delete();
i++;
}
}
}

Read contacts in iOS using Swift with error handling

Just copy and paste the below code Pre-step before pasting the below code Add below line in AppDelegate.swift file below import statement ...