DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets

  • submit to reddit

Recent Snippets

                    //build URI to read barcode
	    //type:  Codabar, Code11, Code128 and Code39Extended etc.
            String strURI = "http://api.saaspose.com/v1.0/barcode/recognize?type=QR&url;=http://upload.wikimedia.org/wikipedia/commons/c/ce/WikiQRCode.png";
            // Send the request to Saaspose server
            InputStream responseStream = ProcessCommand(Sign(strURI), "POST");
            // Read the response
            String strJSON = StreamToString(responseStream);
            //Parse and Deserializes the JSON to a object. 
            RecognitionResponse barcodeRecognitionResponse = gson.fromJson(strJSON,RecognitionResponse.class);
	    List<RecognizedBarCode> barcodes = barcodeRecognitionResponse.getBarcodes();
        
            // Display the value and type of all the recognized barcodes
	    for (RecognizedBarCode barcode : barcodes)
	    {
	        System.out.println("Codetext: " + barcode.BarcodeValue() + "\nType: " + barcode.getBarcodeType());
	    }

	    \\Here is the RecognitionResponse class
	    public class RecognitionResponse extends BaseResponse
	    {
	        private List<RecognizedBarCode> Barcodes ;
	        public List<RecognizedBarCode> getBarcodes(){return Barcodes;}
	    }

	    \\Here is the RecognizedBarCode class	    
	    public class RecognizedBarCode
	    {
	        private String BarcodeType ;
	        private String BarcodeValue;
	        public String getBarcodeType(){return BarcodeType;}
	        public String BarcodeValue(){return BarcodeValue;}
	    }

	    \\Here is the BaseResponse class	    
	    public class BaseResponse 
	    {
	        public BaseResponse() { }
	        private String Code;
	        private String Status;
	        public String getCode(){return Code;}
	        public String getStatus(){return Status;}
	        public void  setCode(String temCode){ Code=temCode;}
	        public void setStatus(String temStatus){ Status=temStatus;}
	    }
                
                        <script type="text/javascript">
/* Source: http://www.apphp.com/index.php?snippet=javascript-toggle-elements */
    function toggle(id, id2){  
        var toggle_one = document.getElementById(id);  
        var toggle_two = document.getElementById(id2);  
        if(toggle_one.style.display == "block"){  
            toggle_one.style.display = "none";  
            toggle_two.innerHTML = "Show";  
        }else{  
            toggle_one.style.display = "block";  
            toggle_two.innerHTML = "Hide";  
        }  
    }
    </script>
     
    <a href="#" id="tlink" onclick="toggle('comments', 'tlink');">Show</a> Comments<br><br>  
    <div id="comments" style="display:none;padding 10px;">Now you can see the comments</div>
                
                        <?php
/* Source: http://www.apphp.com/index.php?snippet=php-download-file-with-speed-limit */
    /* set here a limit of downloading rate (e.g. 10.20 Kb/s) */
    $download_rate = 10.20;
     
    $download_file = 'download-file.zip';  
    $target_file = 'target-file.zip';
     
    if(file_exists($download_file)){
        /* headers */
        header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
        header('Cache-control: private');
        header('Content-Type: application/octet-stream');
        header('Content-Length: '.filesize($download_file));
        header('Content-Disposition: filename='.$target_file);
     
        /* flush content */
        flush();
     
        /* open file */
        $fh = @fopen($download_file, 'r');
        while(!feof($fh)){
            /* send only current part of the file to browser */
            print fread($fh, round($download_rate * 1024));
            /* flush the content to the browser */
            flush();
            /* sleep for 1 sec */
            sleep(1);
        }
        /* close file */
        @fclose($fh);
    }else{
        die('Fatal error: the '.$download_file.' file does not exist!');
    }
    ?>
                
                        <?php
/* Source: http://www.apphp.com/index.php?snippet=php-list-directory-contents */
    function list_directory_content($dir){
        if(is_dir($dir)){
            if($handle = opendir($dir)){
                while(($file = readdir($handle)) !== false){
                    if($file != '.' && $file != '..' && $file != '.htaccess'){
                        echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."\n";
                    }
                }
                closedir($handle);
            }
        }
    }
    ?>
                
                        <?php
/* Source: http://www.apphp.com/index.php?snippet=php-list-directory-contents */
    function list_directory_content($dir){
        if(is_dir($dir)){
            if($handle = opendir($dir)){
                while(($file = readdir($handle)) !== false){
                    if($file != '.' && $file != '..' && $file != '.htaccess'){
                        echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."\n";
                    }
                }
                closedir($handle);
            }
        }
    }
    ?>
                
                        <?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-file-extension */
    function get_file_extension($file_name)
    {
        /* may contain multiple dots */
        $string_parts = explode('.', $file_name);
        $extension = $string_parts[count($string_parts) - 1];
        $extension = strtolower($extension);
        return $extension;
    }
    ?>
                
                    /** Gaussian elimination / Upper triangulization / Back-substitution example by Adrian Boeing
 adrianboeing.blogspot.com
 www.adrianboeing.com
 */

#include <stdio.h>

void PrintMatrix(double **mat, int m, int n) {
    for (int j=0;j<n;j++) {
        for (int i=0;i<m;i++) {
            printf("%+5.2f ",mat[j][i]);
        }
        printf("\n");
    }
}

int main(int argc, char*argv[]) {
    int m,n; //m - col, n - rows
    int i;
    m=5;
    n=4;
    
    //allocate the matrix
    double **mat = new double* [n];
    for (i=0;i<n;i++)
        mat[i] = new double [m];
    
    //initialize matrix
    mat[0][0] = 1; mat[0][1] = 2; mat[0][2] = 1; mat[0][3] = 4; mat[0][4] = 13;
    mat[1][0] = 2; mat[1][1] = 0; mat[1][2] = 4; mat[1][3] = 3; mat[1][4] = 28;
    mat[2][0] = 4; mat[2][1] = 2; mat[2][2] = 2; mat[2][3] = 1; mat[2][4] = 20;
    mat[3][0] =-3; mat[3][1] = 1; mat[3][2] = 3; mat[3][3] = 2; mat[3][4] = 6;
    
    printf("Initial matrix\n");
    PrintMatrix(mat,m,n);

    printf("Gaussian elimination\n");
    //p is the pivot - which row we will use to eliminate
    for (int p=0;p<n-1;p++) { //pivot through all the rows
        for (int r=p+1; r < n; r++) { //for each row that isn't the pivot
            float multiple = mat[r][p] / mat[p][p]; //how many multiples of the pivot row do we need (to eliminate this row)?
            for (int c = 0; c<m; c++) { //for each element in this row
                mat[r][c] = mat[r][c] - mat[p][c]*multiple; //subtract the pivot row element (multiple times)
            }
        }
    }
    PrintMatrix(mat,m,n);
    
    printf("Upper triangulization\n");
    //p is the pivot - which row we will normalize
    for (int p=0;p<n;p++) { //pivot through all the rows
        for (int c=p+1;c<m;c++) { //normalize the pivot row, so that the pivot element can be set to 1.
            mat[p][c] /= mat[p][p]; //divide all elements in the row by the pivot element
        }
        mat[p][p] = 1; //set the pivot element to 1.
    }
    PrintMatrix(mat,m,n);
    
    printf("Back-substitution\n");
    for (int p=n-1;p>0;p--) { //pivot backwards through all the rows
        for (int r=p-1;r>=0;r--) { //for each row above the pivot
            float multiple = mat[r][p]; //how many multiples of the pivot row do we need (to subtract)?
            for (int c=p-1;c<m;c++) {
                mat[r][c] = mat[r][c] - mat[p][c]*multiple; //subtract the pivot row element (multiple times)
            }
        }
    }
    PrintMatrix(mat,m,n);
}

/*
 Another example:
 m = 6;
 n = 3;
 
 mat[0][0] = 2; mat[0][1] = 4; mat[0][2] =-2; mat[0][3] = 1; mat[0][4] = 0; mat[0][5] = 0;
 mat[1][0] = 4; mat[1][1] = 9; mat[1][2] =-3; mat[1][3] = 0; mat[1][4] = 1; mat[1][5] = 0;
 mat[2][0] =-2; mat[2][1] =-3; mat[2][2] = 7; mat[2][3] = 0; mat[2][4] = 0; mat[2][5] = 1;
 */                
                    public static  T getService(ServletContext servletContext, Class serviceInterface) {
  BundleContext bundleContext = (BundleContext) servletContext.getAttribute("osgi-bundlecontext");
  if (bundleContext == null) {
   throw new IllegalStateException("osgi-bundlecontext not registered");
  }
  return getService(bundleContext, serviceInterface);
 }

public static  T getService(BundleContext bundleContext, Class serviceInterface) {
  return (T) bundleContext.getService(bundleContext.getServiceReference(serviceInterface.getName()));
 }
                
                    public static T GetAppSetting<T>(string key, T defaultValue)
{
    if (!string.IsNullOrEmpty(key))
    {
        string value = ConfigurationManager.AppSettings[key];
        try
        {
            if (value != null)
            {
                var theType = typeof(T);
                if (theType.IsEnum)
                    return (T)Enum.Parse(theType, value.ToString(), true);

                return (T)Convert.ChangeType(value, theType);
            }

            return default(T);
        }
        catch { }
    }

    return defaultValue;
}                
                    public static int GetObjSize(Object obj)
        {
            try
            {
                string XmlString = string.Empty;
                MemoryStream MemStream = new MemoryStream();
                XmlSerializer Serializer = new XmlSerializer(obj.GetType());
                XmlTextWriter XmlText = new XmlTextWriter(MemStream, Encoding.Default);
                Serializer.Serialize(XmlText, obj);
                byte[] bytes = new byte[] { };
                bytes = MemStream.ToArray();
                XmlString = Encoding.Default.GetString(bytes, 0, bytes.Length);
                MemStream.Flush();
                MemStream.Close();
                XmlText.Flush();
                XmlText.Close();

                byte[] bytesobj = new byte[XmlString.Length * sizeof(char)];
                Buffer.BlockCopy(XmlString.ToCharArray(), 0, bytes, 0, bytes.Length);
                return bytesobj.Length;
            }
            catch (Exception e)
            {
                return -1;
            }
        }