Convert image into byte array

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Convert image into byte array

    I am trying to insert an image in the word document using RTF specifiations.
    The problem i am facing is that in RTF we have to provide a byte string for
    the image
    I have developed a program in VB that converts image into Byte string. Now
    what i want to do is convert the image to byte string in coldfusion. I have
    successfully incorporated logic in coldfusion, only problem is that i use

    <cffile action="READBINARY" file="mypicture.bmp" variable="pic">

    now i want to convert this binary data into byte array.


    harshalsuri Guest

  2. Similar Questions and Discussions

    1. How to save a Byte array on to the desktop?
      Hi All, I am taking Byte array once clicked and that is being printed in an swfloader in the same page. I want the same to be saved as a image...
    2. #38760 [NEW]: php_mssql fails to convert multi-byte UTF-8 to UCS-2
      From: aireater at gmail dot com Operating system: Windows 2003 Server PHP version: 5.1.6 PHP Bug Type: MSSQL related Bug...
    3. error byte array
      hi I think this code is ok but im getting this bytearray error - and mentions OLE Object <cfquery name="Recordset1" datasource="dependent">...
    4. Convert a Byte value to Numeric - Possible?
      Among the usual suspects: 1. Asc requires (apparently) an ansi value as argument. I'm dealing with the HO bits in a byte. 2. Cint requires a...
    5. How to Convert a Byte to its Numeric Value?
      I need access to the bits of a Byte expression, and the logical functions operatge bit-wise on numeric vales - per the VBScript CHM. So how do I...
  3. #2

    Default Re: Convert image into byte array

    You should be able to do this using java. The standard java.io package is
    useful for reading files. However, due to ColdFusion's data type conversions I
    cannot think of a way to use those classes directly to get a byte array into an
    Array object in CF.

    So... Your best bet may be to write a short bit of java code to read the file
    into a byte array and return that array. I've attached some sample code. You'll
    need to save it as IOUtils.java and compile it, then place the resulting .class
    file in CF's classpath (probably under your root folder, then WEB-INF/classes).

    Once the code is compiled and in your classpath, you can use it in CF like
    this:



    <cfset fileBytes = CreateObject( "java", "IOUtils" ).readFully(
    "mypicture.bmp" )>


    /************************* Begin IOUtils.java ****************************/
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;

    public class IOUtils {
    private IOUtils() {
    }

    /**
    * Read the file into a byte array and return it
    */
    public static byte[ ] readFully( String filename ) throws IOException,
    FileNotFoundException {
    FileInputStream fileIn = null;
    byte[ ] fileBytes = null;
    try {
    File file = new File( filename );
    if( file.exists( ) ) {
    fileBytes = new byte[ ( int ) file.length( ) ];
    byte[ ] buff = new byte[ Math.min( fileBytes.length, 1024 ) ];
    int bytesRead = 0;
    int totalBytesRead = 0;
    fileIn = new FileInputStream( file );
    while( ( bytesRead = fileIn.read( buff, 0, buff.length ) ) > 0 ) {
    System.arraycopy( buff, 0, fileBytes, totalBytesRead, bytesRead );
    totalBytesRead += bytesRead;
    }
    }
    }
    finally {
    if( fileIn != null ) { try { fileIn.close( ); } catch( Exception e ) { } }
    }
    return fileBytes;
    }
    }
    /************************* End IOUtils.java ****************************/

    tkrussel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139