Saturday, May 16, 2015

Java Create Watermark Text Or Fill Area To An Image


package com.pkm;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class WatermarkTextToImage {
    public static void main(String[] args) throws Exception {
 ImageIcon photo = new ImageIcon("100_6929.jpg");
 BufferedImage bufferedImage = new BufferedImage(photo.getIconWidth(),
  photo.getIconHeight(),
  BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();

        g2d.drawImage(photo.getImage(), 0, 0, null);

        AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
        g2d.setComposite(alpha);

        g2d.setColor(Color.white);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
         RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        g2d.setFont(new Font("Arial", Font.BOLD, 100));

        String watermark = "Pritom Kumar Mondal";

        FontMetrics fontMetrics = g2d.getFontMetrics();
        Rectangle2D rect = fontMetrics.getStringBounds(watermark, g2d);

        g2d.drawString(watermark,
         (photo.getIconWidth() - (int) rect.getWidth()) / 2,
         (photo.getIconHeight() - (int) rect.getHeight()) / 2);

        alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f);
        g2d.setComposite(alpha);
        g2d.setColor(Color.red);
        g2d.fillOval(0, 0, photo.getIconWidth(), photo.getIconHeight());

        //Free graphic resources
        ImageIO.write(bufferedImage, "JPEG", new File("100_6929_Watermark.JPG"));
        g2d.dispose();
    }
}

Input & Output Image Respectively

Convert RGB Values Of A Color To A Hexadecimal String And Vice Versa Using jQuery

Hex: RGB:
RGB: Hex:


/* RGB Value To Hexa Decimal */
function toHex(n) {
     n = parseInt(n);
     if (isNaN(n)) return "00";
     n = Math.max(0,Math.min(n,255));
     return "0123456789ABCDEF".charAt((n-n%16)/16)
     + "0123456789ABCDEF".charAt(n%16);
}
toHex("20");

/* Hext Decimal Value To RGB Value */
function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
var str = "#34F355";
hexToR(str);
hexToG(str);
hexToB(str);

Friday, May 15, 2015

Create Zip File Using Java From List Of Files


package com.pkm.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static void main(String[] args) throws Exception {
         List<String> fileList = new ArrayList<>();
         fileList.add("ImageName.png");
         fileList.add("ImageName.jpg");
         fileList.add("ImageName.gif");
         createZip(fileList, "Output.zip");
    }

    public static void createZip(List<String> fileList, String zipFileName) 
              throws Exception {
         FileOutputStream fileOutputStream = new FileOutputStream(zipFileName);
         ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
  
         for(String inputFileName : fileList) {
             File file = new File(inputFileName);
             FileInputStream fileInputStream = new FileInputStream(file);
             ZipEntry zipEntry = new ZipEntry(inputFileName);
             zipOutputStream.putNextEntry(zipEntry);

             byte[] bytes = new byte[1024];
             int length;
             while ((length = fileInputStream.read(bytes)) >= 0) {
                 zipOutputStream.write(bytes, 0, length);
             }

             zipOutputStream.closeEntry();
             fileInputStream.close();
         }
  
         zipOutputStream.close();
         fileOutputStream.close();
    }
}

Using Java Create An Image And Save To File

package com.pkm;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class Main {
    public static void main(String args[]) throws Exception {
 try {
     int width = 200, height = 200;
     // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
     // into integer pixels
     BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
     Graphics2D ig2 = bi.createGraphics();
  
     ig2.setColor(Color.red);
     ig2.fillRect(0, 0, width, height);
  
     ig2.setColor(Color.green);
     ig2.fillOval(0, 0, width, height);
 
     Font font = new Font("TimesRoman", Font.BOLD, 80);
     ig2.setFont(font);
     String message = "PKM";
     FontMetrics fontMetrics = ig2.getFontMetrics();
     int stringWidth = fontMetrics.stringWidth(message);
     int stringHeight = fontMetrics.getAscent();
     ig2.setPaint(Color.black);
   
     ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4); 
   
     ImageIO.write(bi, "PNG", new File("ImageName.PNG"));   
     ImageIO.write(bi, "JPEG", new File("ImageName.JPG"));   
     ImageIO.write(bi, "gif", new File("ImageName.GIF"));   
     System.out.println("Image Create Done");  
 }  
 catch (Exception ex) {  
     ex.printStackTrace();  
 } 
    }
}

Output


Saturday, May 9, 2015

jQuery get an elements position or offset according to some parent element


$.fn.offsetRelative = function(top){
    var $this = $(this);
    var $parent = $this.offsetParent();
    var offset = $this.position();
    
    // add scroll
    offset.top += $this.scrollTop()+$parent.scrollTop();
    offset.left += $this.scrollLeft()+$parent.scrollLeft();
    if(!top) {
        // Didn't pass a 'top' element
        return offset;
    } else if($parent.get(0).tagName == "BODY") {
        // Reached top of document
        return offset;
    } else if($($(top), $parent).length) {
        // Parent element contains the 'top' element we want the offset to be relative to
        return offset;
    } else if($parent[0] == $(this).closest(top)[0]) {
        // Reached the 'top' element we want the offset to be relative to
        return offset;
    } else {
        // Get parent's relative offset
        var parent_offset = $parent.offsetRelative(top);
        offset.top += parent_offset.top;
        offset.left += parent_offset.left;
        return offset;
    }
};

JSFiddle link


Friday, January 16, 2015

eWay Shared Rapid Payment Using PHP

First, create an api from eway api setup page for online hosted rapid payment



Then download source code.


Browse http://localhost/ewayHosted/index.php and fill up Api Key & Password field and following by other fields and click 'Pay...' button. 
If all details are correct you would get data like below from eway:


stdClass Object
(
    [SharedPaymentUrl] => https://secure-au.sandbox.ewaypayments.com/sharedpage/sharedpayment?AccessCode=F9802jxD7ElfeXKsdljElP1OKxSJyKZHJuaRB8qf97fzPPiDGFS-JmlfVug7kAvkctMALC0cSrVtKdomIp0DPlJv0CdRAl4apAHgJNzFMo5TXfQ804a8-5BP80gWFSmOLsLgeXOGFrjiuBvfYPjjs8F1zBw==
    [AccessCode] => F9802jxD7ElfeXKsdljElP1OKxSJyKZHJuaRB8qf97fzPPiDGFS-JmlfVug7kAvkctMALC0cSrVtKdomIp0DPlJv0CdRAl4apAHgJNzFMo5TXfQ804a8-5BP80gWFSmOLsLgeXOGFrjiuBvfYPjjs8F1zBw==
    [Customer] => stdClass Object
        (
            [CardNumber] => 
            [CardStartMonth] => 
            [CardStartYear] => 
            [CardIssueNumber] => 
            [CardName] => 
            [CardExpiryMonth] => 
            [CardExpiryYear] => 
            [IsActive] => 
            [TokenCustomerID] => 
            [Reference] => KU060238
            [Title] => Mr.
            [FirstName] => Pritom
            [LastName] => Kumar
            [CompanyName] => WEB ACTIVE
            [JobDescription] => Developer
            [Street1] => 15 Smith St
            [Street2] => 
            [City] => Phillip
            [State] => ACT
            [PostalCode] => 2602
            [Country] => au
            [Email] => 
            [Phone] => 1800 10 10 65
            [Mobile] => 1800 10 10 65
            [Comments] => Some comments here
            [Fax] => 02 9852 2244
            [Url] => http://www.yoursite.com
        )

    [Payment] => stdClass Object
        (
            [TotalAmount] => 100
            [InvoiceNumber] => INVOICE1001
            [InvoiceDescription] => Individual Invoice Description
            [InvoiceReference] => 513456
            [CurrencyCode] => AUD
        )

    [FormActionURL] => https://secure-au.sandbox.ewaypayments.com/AccessCode/F9802jxD7ElfeXKsdljElP1OKxSJyKZHJuaRB8qf97fzPPiDGFS-JmlfVug7kAvkctMALC0cSrVtKdomIp0DPlJv0CdRAl4apAHgJNzFMo5TXfQ804a8-5BP80gWFSmOLsLgeXOGFrjiuBvfYPjjs8F1zBw==
    [CompleteCheckoutURL] => 
    [Errors] => 
)

Then collect 'AccessCode' from data returned and redirect to 'SharedPaymentUrl' where your original payment would process.




Need to fill up all fields and click on 'PAY NOW' button above.



Now click on 'FINALIZE TRANSACTION' button to redirect back to your original site from where you went.



And finally your transaction in eWay portal:



Php & MySql oAuth2 Server & Client Example

Download source code and install


Add a new client secret pair to server side:


Start authorize from client side:



Authorize application from server side, add your login functionality by own to make the page restricted.


Now, you are getting your data from server using the access token. After expire access token, program automatically can collect new access token using refresh token.