将十六进制字符串转换为图像

时间:2014-11-18 10:32:46

标签: java php image hex

我们的客户使用java代码从数据库中存储为varchar的十六进制字符串中呈现图像。我们想在php中创建一个类似的代码来渲染相同的图像,但是无法这样做。以下是客户端使用的java代码示例。

 variable "photo" is hexadecimal String fetched from database. Further code is convert it into image format
try {
   String photoStr = (String)request.getSession(false).getAttribute("photo");
        byte photo[] =null;
        if(photoStr == null){
            photoStr ="0123456789abcdef";
        }
        photo = new byte[photoStr.length()/2+1];
        int i;
        String str1 = "" ;
        int cnt = 0;
            for(i=0;i<photoStr.length();i=i+2)
            {
                    if(i+1<photoStr.length())
                    str1 = photoStr.substring(i,i+2).trim();
                    else
                    str1 = photoStr.substring(i);
                    Integer val = new Integer(Integer.parseInt(str1,16));
                    photo[cnt] = val.byteValue();
                    cnt++;
            }
        BufferedImage bufferedImage = new BufferedImage (165, 165, BufferedImage.TYPE_INT_RGB);
        ImageIO.setUseCache( false );
        bufferedImage = ImageIO.read (new ByteArrayInputStream(photo));
        ImageIO.write(bufferedImage, "JPEG", outb);
} catch (IOException e) {
        e.printStackTrace();
  }catch (IllegalArgumentException e) {
     try{
        String fontName = "Arial";
        final int fontSize = 16;
        AffineTransform at = new AffineTransform();
        final Font basicFont = new Font(fontName, Font.PLAIN, fontSize);
        final Font font = new Font(fontName, Font.PLAIN, fontSize).deriveFont(at);
        BufferedImage bufferedImage = new BufferedImage(165, 165, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, 165, 165); 
        g2d.setColor(Color.RED);
        g2d.setFont(basicFont);
        final FontMetrics fm = g2d.getFontMetrics();
        g2d.setFont(font);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        Rectangle2D rect = fm.getStringBounds("\n\n\n     Photo is not available           \n\n\n", g2d);
        int drop = fm.getDescent();
        int width = Math.min(165,(int) rect.getWidth() + 2 * 2);
        int height = Math.min(165, (int) rect.getHeight() + 2 *100);
        g2d.drawString("\n\n\n         Photo is not available           \n\n\n", drop+1, 125);
        BufferedImage croppedImage = bufferedImage.getSubimage(0, 0, height, width);
        g2d.dispose();
        ImageIO.setUseCache( false );
        ImageIO.write(croppedImage,"JPEG", outb);   
    }catch (IOException e1) {
        e1.printStackTrace();
    }
 }finally{
 out.close();
 }
=

以下是我们用来渲染图像的php代码

<?php
    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("image");
    $sql = "SELECT * FROM tbl_image where tbl_img_id = '14' "; 
    $result = mysql_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
    while($row = mysql_fetch_array($result)) {

        echo $image = base64_encode($row["img"]);
        echo '<img src="data:image/jpg;base64,'.base64_encode($row["img"]).'"/>';
    ?>
        <!--<img src="imageView.php?image_id=<?php //echo $row["img"]; ?>" /><br/>-->


<?php       
    }
    mysql_close($conn);
?>
</BODY>
</HTML>

以下是存储在数据库中的十六进制字符串

FFD8FFE000104A46494600010100000100010000FFDB0043000B08080A08070B0A090A0D0C0B0D111C12110F0F1122191A141C29242B2A282427272D3240372D303D302727384C393D43454849482B364F554E465440474845FFDB0043010C0D0D110F1121121221452E272E4545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545FFC000110800CD00CD03012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F004C52E38A434EAD4CC4C60D07AD2D26280140A514DC52D0314D368CD349C52014B051927000E4D460B4A9BDC154C67078E3D4FA550D57548AC94464179B1B8478207B127D3BE0726B99B9BE9EF176DC4EF20EA54F0B9FA0A571D8E9AE75CB2B75288E662BFF002CE2071FF7D631F966B2A6F125D3B0F2112DF0720801CFEA31FA563023BD3C08D81C87071C6DC1C9FA50D8EC5BFB6CCCADFBE9D7712CC558ED39EBF28E3F2A6148645C87323B1E823EBF9F3504424C12A0B0EE050CAAAF9DFC1EF8C52B8872AAE70000071481842FBA33B5BD578A66FDA7208CFF003A6BB2BF2A369F4068197E3D4AE1A689A49BCC08783222B103EB8AD04F11CFE611E4AC884F05885603F0007E95CF6E20734F525B1E945C2C76365AAC776324043D319CE2AFA9CD70714BB640CB232303C15ADBD3B5C11ED8AF70AA4F130C9C7D40FE62AAE23A3268CFBD461F207B8C8C1C823D69DD69885CE6824D34647539A534087062282E4F5278F7A68A4A063B14BDA90D2E28105146683400019A08C526690B5218A79200EA7A62B0B58D60441ADACE50670D892451909EA07A9FA74A35BD59228A4B4B6626663B64753F707719F5EDEDCF7AE7102A8C01803A014AE3B092BB48C59DDDDCF5676249FC4D33A72695A463F2EE217D2933F37AD21803807141E474C1A5DA50E58647F3A181F4214FDDCD002A92A448188607B70697E790B3820E0E5B245349C3003807AD39470541CE3A5002C84100F393ED511E9CD498DDDB07D3D69AE7FC9A04478C9E0F14ECE169A0639CD38B7CA067F4A0601883D29CADF353338E94AB8CFAD023A3D06FC616CE4F7311F4EE57F9915BEA4E2B8485DA291245F9591830FC0E6BB4B6BA4BAB74B8847C8FFC27F84F71F853B81383CD140028CD5085E82933451C5003F34B9A4EB40140809A3B5291C52503131597ADDF9B2B40913159E6C8561D54776FE83FFAD5A5248B1AB348C151416663D80EA6B88D42F1AFEE9A760173F2A0EB851D054B1A2B170BD72C7AE4D34BE4E41C0F4A339ED9A6F7C52181E4D3D57B753EB48133CE7156228FE701B91D8D26C1222219946738EA07AD295053180BC641C77F4AD2874FF308642AF1B7279C15FA1E869AD6EC817CBDCCA33D57247A8C8EB4AFA0ECFA19E136ED3F78119041A7796496E0823B55D9A074D9FB97563D1B046E1D2A116F2312149C7B8A1490F9591451BCADB10839F5A8E48886F9866AFC7A7CB90C62C8FA56B41A0BCEAB208D4FB331C7F8D4CAA2452A6D9CD1B57F244A4610B6D1EE7FCE29AD132E318C1EE0E6BD0ED743486D823A64918CE318E73C7E3585AB6886D6E06E21A19B76D70BCAB019008EF510ACA4EC5CE8B8AB9CB8183CF2680327D2B42489555519398CF51C6E079C7D460D41E57EFA4310CA063B40F4EBDFDAB7301AA76E46DFD6B4345BE36B76B1337EE66386C9E01EC7FA7FFAAB3D807DC471CD229539571C11C10684267779EC7F1A5AA3A5DD35DD923487F7A9FBB7F7C0183F88C55ECD5882939A5A423DE801E7AD381A69F6A2801DEB4D34B4606280286ACA5B4ABB551B898C9C0EE0727F95714460FBD7A11019977FDDCF35C0CD248D2C809270C47CC738E7A54B190939ED42E41E334A086230306AEC5A74D2C7BE3DAC472533838A9BD8A4AE5705A5901624B1FCEBA6B1D2DE7B459224DC8C7AB3E1BA73C541A7F87AF65903344B103C0676076FBE01C93ED5DA5969E9042A88E415180D8CD73D4ABD8E9A74B4BB3297C39712C61BED1040BD596440FE67B0EFF8F15A1A4F812D8C11DC5F5C039F98410C785F6C92493F862B5A0B4DC4F98630A78658E20A5FEA739C7D2B5D18914936D6A37149E860BF86E25395966DBDE357E0FE79A8D340B1899956364CF396452C3F1239AE8883DA9B822A794AE631A1D1EC6170E96CD3C83A4936320FA800003F015685AEE3923A55E23D69370CE28E5EE172AFD9971D71591ADD9096C26D9C488372F1DC735D03B0C567DC004373D4629356D8A8B3CAB506DF2195460E7820F5C75AA8B3145E08073BBE95D15FE9071788AA7319FDD7B9E58FF3C5735383E6631C000574427CC72CE1CAC630E7208C67A8A4CF18C669A063B820D39482707AD6A8C8DDF0E4986B8403390AD8F61C7F515D00E6B97D09B6EA6839C3230381ED9FE95D40E6A912C776A4C52E38A314C0534A38EB451400B487A51CD148069C815C46A96EF6DA84E8E00CB975C1EA1B91FCEBB839ED5CD7892D0ACD1DC8525186D76EC0F6FD2931984BF78100715ADA73E64E36A990E32719AC81C1AE8B41884B7D6FBBE65452D83EBFE456351D91B525791D858C4122404671EB5AD12E4640C5528970A38ABA67486305CE33C01DCD71A476B2DC7C919AB49C719AC43AC59C041BAB986DD39C97719E3D87357EC353B1BD4CC17703103246F19ADD5EC64DEA5D66C526FE2A7784AF25481EA462A3318C5362222D4C66A9768C609A6B22F4F5E05202069062A9CC4355D7B57C121188FA566CD2018F46E9EF59C8B451BC8F721C75AE075AB416F7A76AE04837E3D0E791FD7F1AF42670724723D45616A9611DD4C8EE33B0E4827861E94A9CB95EA3A90E789C230E71C8A7C6B9C7A773562FA116B7B2C483015B8C8FC7FAD55EA7939AEE4EE7035635B4051FDA679DD8898E7A6391FE35D48E95CEF86D0F9B72F9380813AFA9CF4FF80D746BD055A218A290D2E714879A603A814DA728E280034628239A08A0092087CE72A5B62AA9666F402A85EDBD8EA163793DA3DCC91DB062ECF1911B285249539EA0E3B5695AC62533C0C322585947BD5AF0E4D8D26C03207511B6F8D80208DCC307EA2B8EAC9F3D8EEA505ECEE79A5DDAFD96E1A3DC1F1D187435D0F84ED58BBDC31F947CA00F5FFF005552D76CA0B4BD75B388C50BB164859C3F97EC0F5C7D6BA6F0FDAB41A7A2B8DA49CED3CD1525EE934E369976F2FBEC50E55434AC76A0738507D4FF0085561A6EA9A8C8A6599218CAFDE62C33ED803F91AD34B3FF004833364E06179FBBF4AA5A9DF5D99A3B6B54DC643B4832796589EE5F1C28EA7B9ACA3A1B4B521B8F0EDB20064D44C5302371255B38EDCF207BF5AC3B8D2DE12554DBDD2F42D0B0C919F4383F97A569F8A2D754F0F0B585F555417513B325ADB2C68594AFCA18659BAE4963E950787ED354D7C5FBC57C2E5ED5236115CAE5652D9CA6EEAA78E0F4FE75BFBC91CF78B64DA68BDB19F12F9E91B919C1271EFEDF85751A6EA52CF1C6B2125DB2338ACDD391CDB86DAE831929270CA7B839F4AD8B28D77060003EB597336CD796C2DFDC3C4BF29C13D0FBD733A9EB2EA4076DAA80F0A7A13EF5D35FE0A7CC060572D78400CC3CA8D472D2CA32A83B93EBF4A39ACC695D1CD5D6A12DC921AEA7941EC5D8E07E079AB567A76A283365692451B72DBCAA6EED903AE6AE6A325E69496F2A69F129BA4792396FA42D2BA285CB797B82A0F9B8182DF8D3347BCD62FDAE56DD2299E1896568E29763ECCE32A394279E41E7F1CD692BDAF63156BEE2FDAEF34E23ED505CC0C4FCC6E22E0E7DD78FCEAF8B98AF2312C4C0F6201E952D8EA82FA230C8199413FEB23C107B865EC6A092C843786788902418750063DAB09599D30BA393D700FED3948EE14FE807F4ACF033D6B5B5E56FB764F4D831EFD6A8DB445D8314DC83A965DCA0FA1AEA8BB451C738DE6CD9F0D99045709E5E232C1B713DF18ADD1C74AC9D2A4712BC1222AB21C8DABB411E98AD6CF6AD612E64673872B17AD078A502908AB204EF4EC914CA7039A00773DE8CD266968026B59043770C8C785719CFA679AB5A4C22D2D2E95B816F23463FEFB635431915A114865FB520DBFE991875DDD04A9D7F3AE3AEB54CEEC33BC5A38FF116C5D554ED0CCEA2420F7E48FE95DB430846D806029C5723E218DE7B1B7BBD92896DFF00752C6C5708092411F53DEBB1B56F31438180E030CFB8CD672D522F6932E0185007534D9F4F13C2548383D7153C310CE47D79AB8A314D444DD8E7AFAC0CF629652C1E75A2B0611C8DBB6638CA93CA9EBD3D6A2B386FACED05A5BCB25940096096ACA096CE492D824FE27F3AE95C2B0E82A2D8BE9CD56A2F77B19A558876919DA4739258E6A7B70CAA31D6A57404D3E2519ACFA9657BCC98C9C561DC65446557957C960A0E3B8383D79ADEBC216223B9ACA0824240EBE94AFA94B62AEB625D7EC628AFA25B892125A29E35F29D73D411C820E07A52E8B6E9A3D9C90D846CB25C10669E53976C67000E8A066B62DE05DA01AB42DD718AD79A4CC7922B530D2CD8CED2B12CCDD73CE691E2D9E6236481CA13E95BCF1281C0E2B32E9300F1C564D1AC59C2F8862DB344E14F20AFF9FCEADE8D666DB4C9A4E3CC981E839D981C7E755BC4B20DD12752096C0AD8B0262827DC095821E4039E7A01F9D549BE448984573B6451C40EA57B21248565895BD4A8F9BF5AB2060D3218FCA88275392CC7D58F269FD2BAE92B44E5AF24E7A066909A0515A980528A4E94A0E6801D494A28E9400A2ACDB36E5319EA183A1F4355AA489F64A8C79008C8F5159558F344DA8CF9643FC53616EBA14D751232CAA53CC1BBE52B9C13CFA66A5D02532E9566C78FDD6DFF00BE495FE95A9188AF7CA81D9258AEE391648F3FC000073E9F7BF4ACED32C9B4D812C59B71B7DC9BB1D7E6273FAD71BB58EBFB4D9BF11000AB19C800F19AA511F9706AC231079AB4C4C9B18E2861F2F4A36938C1C0A748C150D508A521E719E69A99A8A357790B36715661425B0064564B72F629DF1F900079AA08BB9F18ABDA846C8467EA2A8DAB95BAC3746152D6A5FD9362251B062A75150C5C818A559BE6E7A56C8C47C8702B26F9C043CD5DB998F6E2B0EFA7CA37351265C51CACCBF69D6E5254388B6ED0DD33C1E7F135B30C622B78E1059893E65C3B1FBC7F8540EC3351F87625FB2DC5E94477B8909466E4AA82471F539AB0ADF291DCB124FAD16E69288297241C83181498F5A53484D7A08F39862969051C9A0425387029318A5140052D20A76280018A5233CD18C0A519E28191CB6EB3019796323F8A190A37B8C8E6B52C4F3CE49F52727F3AA1C8C55CB139635CB5A292BA3A28C9BD19B119CD59439E3D2A9C67231E95346C09273802B14EC6E5E66D80605452B0DA7BE6AB998906424055EA49C01F8D62EA5E28B2B54916376B99D47DD894ED07D376307F0CD588BD34B710CA8D1DCEC887DF88A290FF00891907E9566D751431B1078E848AE0F52D6AF6FAECC1BBC8453828AB820E39C9EB53E9F15C46D3BF96033C78531A063D7B91D28E5B0EE9E874DAAEAB13FEEADCADCCCA33B15B007D5BA0A82CDA5721EE9228DC0FB9131603F135C5B5CC9138970AAC1B71D9C727DAB774CD790B05BC28A703E600E09A8945A771A926AC7596E79EB4F9180273F855286E15903A9041E8454F2CAAC80E68B8AC55BB94F007E75897CCACA55B3B4F0DB739C77E95A13C85DC9EC2A84A7F7D1E3A839A95EF4AC5B7CB1B890DEDBBB15B081D6D56011A3952811830E80F24E320E7D73934718E2959998E58E4FA9A69AECA74D40E2A951C843EF494B9C8A0F02B6310CF1C53734A290D002D2D14A280169453697B5002D2E78C5341E68340C77156ACDC09B19EB5533C5391B6B023A8ACEA2BC4BA6ED237831078A237F948F7AAD1CDBD322A48A4DCD815C276D8CEF10D95C5EA2C7046658CA90E9B8024E4118C903FC6A8DA58E996BB5BECD75E681D2597E407E8062BA27273D7349BB382C067AE4F73549BD8124F73056C34F378272D0B0618685E52558FE7C75AB2DA3E92EC928B29212A41D96D70C15B1F5F5AD9363115E0A3AFBA8073EF51BE936814928A44872CA198608E9D0D5D996944E7EEB4BB19D0A436F24783D5A72E47B024565BF87D370DD791C6C7390F11008EDC83D7DEBAABBD16D4206686245E809E4FEB59FF0061B22EA0C0300FDC04807EA33537681C22CC5B03A9D9C98B5026418568A275C3283C0C9E0115D4A4ADE57CEBB5BB8CE714B1AC70A8582248947645000AAD3BED381D49CD449DC51561247EC071EB5501DD33B0390A3029F3CDB10E2A344DA817BF535A518DE5732AF2B46C2F6A4E29DC629BDF35DC70887AD19E694F3498A004CFAD21E69D8A4C5002F7A514DCF34F18A005E949D4D18A280178A33C52668CF140C0924E00C935CF6AFAB3CA5AD2C9D829E1E443CB7B0F6FE7F4EB635FD41ADEDC5BC4E564981DE47509FF00D7FE87D6B0F4B8566D4AD22232AD2A8F4E339FE42B39BD0D22B548F462857734633B4E197D69D0C819C329E3B1A75B121B3EBCE68BA8046DF6884607F1A8E9F5AE0B1DCF42D904F348D0798B8CE0FAD321943275AB22401F6F15695C8BD8A66DEFE36F9258997FDA53FCC1A66ED57243456CCA7A959083F962B5CA8DBC9A8D92360CBCFD4556A57318F35BDFCCF9710A2E3A8624FE5425B88D49625988E31EB5A6C04499C93B4704F26A191C3203EB52C398A8C30BCFE959F34837B1CF03D6AC5E5C845C64565C84B292DD3B0F5A82912C6DE713213F283803FAD499CD57B49524B5468DD586483839C1CF20D4C2BBE9C796279F524E521FDA9A79A09A01AD0CC3A0A0514940077A5A4C52D00277A503DE93AD380A000529E9C500506801BDE90F3C52B304193C54125CF976F34FB5731C4EE07AED527FA54B7637A749CB5E8727ABDCFDAF529E453F286D8B9EBB4703FC7F1AB7E19B7336AE9260ED8559891EB8C0FE7FA56390723775AEC3C256F8B579700191F19F61FF00EB358D4768954D734EE7556E9F28CD58201183490AE1454A5476AE64743334C6F6F21D9CA9E40A956E148CFF0010EC7AD4CEB93CF5149E4452E3CC404FA838345C4D0BF69DDC134A27D8A79CE6ABC9A513CC57972ABFDC3B587F206A13A75D82717031EF1FFF005EAAE2B13CF75918154EE2F9638C283F37A5326B3913992E189F4540BFE354E4455E003EE4F26A1B2D2198695B7BF4A8E739FAD5954F938A8258F2C067AD25A956B2397B3D41EC6F9E40331BB112267EF0CF5FA8ED5D6230650CA72AC01047706B8572771E3F2AE8BC3F75E65AB5B91CC472BFEE9FFEBE6BD04EC79ED5CDA3D28148067AF14A3F2AB3303C521A5269334C0290E4D3A92900E14BC2F278F7A8F7169E285700CADB771E71F8569C76B1C37174BCB7D919159FF89CB11F77390A39F427DC5008A91C334EBBA082595738CA29233F5E95626D32E2D2DDAE2F9A3B6894671B848EDEC029C77F5AD3D55EDF47D2A6BFFB2ACE62525848E59A4F41B9B71033CE0563EB97B34CF6D0B37EED631295C7DE9373293F4C0E076C9EA79A87236A54F9A4932965E450ED901B85047A75AA9A9954D2AEC938CC4538FF006885FEB5780FDC03E82B235F72BA5AA8E924EA0FD0063FE159EECF4AA251A6D239B6C6062BB6F0910DA58C1C959181F6E87FAD714C7815D7782C911DCA76DC1BFA54D5D8E1A3BB3B38C714F20D471F6A909E2B03722917241148A31C548693BD201D1B6D206783D8D3669B68240C531DCA91DEAADCC864E08C50C12209C973927159EC37C98157A51FBB27D2A282304E4F5A8345B0AB1009923A55498725B15A12F0B5427FBA79AA40F6382950C733A8FE1623F5ABBA3CE60BF8C0C9127CA47F2FD6993C624D51E33901E423E9CD56873947070410C2BB7A1C11D247701B2A0D19A644731FB838A7F4AD16A8CE4ACEC069693AD1DE992028279A3A51408FFFD90008000F0030303030303233303635393031383308000400312E3030080011003531303531333231313930313730303738

2 个答案:

答案 0 :(得分:1)

以下应该工作

<BODY>
    <?php
        $image = $row["img"];
        echo '<img src="data:image/jpg;base64,'.base64_encode(hex2bin($image)).'"/>';
    ?>
</BODY>

修改 解释它是如何工作的

  • 首先从数据库中获取图像为十六进制字符串
  • 将十六进制字符串转换为二进制
  • 将二进制编码为BASE64字符串

答案 1 :(得分:1)

下面的代码可用于从十六进制字符串值中提取图像

<?php 
    $binary_string2=pack("H*",$hex_string2)
    ?>
     <img  src="data:image/jpeg;base64,<?php  echo base64_encode($binary_string);?>" alt="IMG DESC" >
     <img  src="data:image/jpeg;base64,<?php  echo base64_encode($binary_string2);?>" alt="IMG DESC">