utf 8 - Convert a sequence of bytes to a jpeg file in Java -
i interfacing medical instrument pc using java . data transfer working fine. problem along content (string), data contains 4 jpeg images . entire stream encoded in utf-8. problem able extract data jpeg unable save file in right format.
i have looked around , data looks fine (in starts ffd8 , ends ffd9)
i'm storing byte data in array _tmp. have tried following
1) writing bytes file
fileoutputstream _fos = new fileoutputstream(_filename); _fos.write(_image_string.getbytes()); _fos.flush() _fos.close();
i following error when try open file
i have tried use bufferedimage follows
bufferedimage _bi = imageio.read(new bytearrayinputstream(_tmp)); file _image = new file(_filename); imageio.write(_bi,"jpg",_image);
for following exception raised.
java.lang.illegalargumentexception: image == null! @ javax.imageio.imagetypespecifier.createfromrenderedimage(unknown source) @ javax.imageio.imageio.getwriter(unknown source)
the spec of data format pasted below.(even though mentions bmp data - documentation mentions in jpeg format)
i realize has encoding. not able figure out. pointers appreciated.
your jpeg data in string "ffd8ffe0
....ffd9
"?
(no spaces between hex values ff d8 ff e0
)? remove whitespace or non-hex chars (valid hex chars : digits "0" "9" , letters "a" "f").
convert string bytes
//# import class import javax.xml.bind.datatypeconverter; //# declare byte array public static byte[] imagebytes; //# string imagebytes convert with... (writes byte integers hex values) imagebytes = datatypeconverter.parsehexbinary( my_hex_string );
now imagebytes
can saved file or used (input) jpeg data in other functions.
some example code :
this should output below valid jpeg string:
byte value 0 : -1 (which == 0x
ff
)
byte value 1 : -40 (which == 0xd8
)
byte value 2 : -1 (which == 0xff
)
byte value 3 : -32 (which == 0xe0
)
package hexstringtobytearray_v1; import javax.xml.bind.datatypeconverter; public class hexstringtobytearray_v1 { public static string image_hexstr = "ffd8ffe000104a46494600010200001000190000fffe00104c61766335372e34382e31303100ffdb0043000806060706070808080808080909090a0a0a090909090a0a0a0a0a0a0c0c0c0a0a0a0a0a0a0a0c0c0c0c0d0e0d0d0d0c0d0e0e0f0f0f1212111115151519191fffc401a20000010501010101010100000000000000000102030405060708090a0b0100030101010101010101010000000000000102030405060708090a0b100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9fa1100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffc00011080040006403011200021200031200ffda000c03010002110311003f00ec3c4575abdbcd64b66275b46137dae6b4b782eaed1c05f25520b8754f2dc96dee15d8100606735a97963f6b20f99b308571e5a3f2594eecb73fc38c74af5ea3a978f2dedaddc526fcb47d0a9439bafe07c5e1961dc6a7b471e7f779235272841ad799b9455eeb4b2d11953abecfa5f5beed74f2393d4b51f1447013a68bc9cff65dd3ab5d595ac5706fd6f215850c2b841fb832955e8c064f35d2ff006381e6e25037eedbfb88bf761893819fbd827bfa56529564bdde67eebde293e6e656d3d0bf67bebf823ae94702e5fbd9538fefa0ad0a9371f67c92727cdbfc56bf639beb0fddd36b5fde7ad8cbd16f35a975211c86f26b0fb33b4b2ea3676b67325c865f2d2dd6dd8195194b1937c602e061b9c56c5be9a96d30955871e6fca2251f2c9b30a1872020403fdaef4a0ea736bcce36d5ca2a2efe56dca8c395dfd7a7735af1c37b2bc5d35579d5a34aa4ea45c2ceee5ccb469dad67af6319d6738f2bbfd9eafa5f5f9dcbd59daeeac9a1e9b717aea6431a80918eb24ae7089f89ebed5626ec9bec645422e728c5349b76bbd9776fc92d4b971730dac6659e5485075791828fccd78ddaeb73eb37325c6a1299e4193e527faa8107f7a46c451aaf7dbf89a61879a937cdadba2d92fef49e8896edbb1e67879528c552f754b4e79fc536fa53a71f7e57e9f91e99378c7438325aece07f12c52b03f4c2f35e2faaf8bf4fcc9b3edb7312b7965acc2c300241c21b9911e466201fbaa8b81f2e7ad36acaeda4615f1f46ed5e73ff00af768c7ff0292727f249111973cb9631937e9a7e363d0c070ee3b923271c3d17bffb473d59ff00e0aa6e34e3e92729773d813e20f859fa6a91019c162930553e8ec530a7eb5e31a32d9f88fcdb7b2d1aed1580135d3de0291f5eace9c9ff00657e63e957ed61fcf1fbcc294e8d7bc63426afbcbda5edf7a30784c4a76f6352fba56dd775dfe47a18ba18fcbf92ad5cc284f97e0a4b0ce2e5e49465b79bd0fa1edaea0bc8527b79639e2719592260e8c3d9864579cf8474ebef091741782e6d1ffe5d9964f94ff795cbb053f44e7bd755ee2a547d92b73de3d37bafc4f25a716d34d35ba7a34698bc63c64a33587e4a9f6da6b964bd1453bf9b6cf4bac44f12c19fdec3220fef290f8fc30a7f2a63692ea64115397d968dba6c6eb2c692a1dd1b80cae3ee907de909493d9a02a74e70f8a2d6dd34d7cf61d453024028a000028a000028a0000b56964f759c1551ea4f3f5dbdff004a8fed530c6d6d98007cbc640e99ed58d6c42a3d1b7fd752bd8c1deeafd75d4edc0e5d531b7b38c5776f5f551ddfe08c9636bc6dcb3e46925786974b6bf4d0d03a3053ccdf2fd39fe78ace92e269461dd987a1ae7faf37b435f5d0e98d2841dd4523d379028bf7b10943bf2ebf8bb1e555c557acb96a54949766ce27e252cd3597d82dd8664219262ae72ca595936a0255429c97ce3b715d2ea5a645a9468aced1b293870b9c03d46091907ea2ae309d6a326bdd93b24b57bebd3c8aa75654ef649f95f7ff00232a95e860b1d4d4af56941ca4e578c2f6d1692766efb2d6fba31c46169e25439a4e2d3dd46f6f95d5fbf43e76f18b3689a7d8e936ee713c667ba947ca6621b0138e880f3b7e95e97aff00807c35aa5dda0d4359bbb616f2184a1b270263211b52398160a4b636e41ce6b8f1ff00ecd4e9e1e2fe25cf37b736ba2f42331e793a73945455b955a5cde7af63dde1e4b34c462732ad1d69d4f638783d5524a29ca5afda774ae6fc2ea852a788a34aacea494d54973d2f67d141b8eaeeaf6bf63c5acf5abf974f8f4154b696da6ba0ea1e056956690850c920c4808ed8e7923a715f57e85e1cf0668a9b2c74eb485a40a5a431e642546d04b3ee653df03033938cd7101ef0cf2f6b3bad16c6dadb4d898c71858ff7620e38e6595a6de4863cb6d4635b27ccb5965825eb148c9f829e0fe239ae9559d28a50dbcac797f58716e2fa3b1e74b2e589a8e557de93e8eebe4ad6fccfb4595d3c4421569eaa715256f339bd2754f124f7d3dbdedbdb35ac5ff2f48a577e7a796eadb1f1fc5fbb5ad2d4f5fd36cc6c9350b285c9fb92cbf37fdf2a770fc702bd48e32a3ded63cd556735eea6cf8b9645417c2a517d15ef7fd4fae9e130d869daad4a709769495d7aadccdd7b5fbcb07920b3d39ef258e369646276451c6a32cd9e37633f754e4f6a4b0d593519ae1cfd9678ddc24696d20b8dec381d87ccd9036e2bd2962dad0f2a5564a4959ddf4b75f23e4a192c6ca4f9acf5d11f6b43094aa5194d54a5ece9dfdf8cd34a2b56e5b5ade67a57c24d44eb9e1533dc84606fe708ab1c91aa80b192a048ee4e189f981c1cd749a1d90d1b48b681f646c10bc8131859243b99463aedc85e3d2bd15566e5ce9d9eda0b0f4e5651b7bdd7d4f8eab85c3aa6a94e3cf1bf35a5dc79ae2e94aad4ab19af657b45dfecad13f9eff3197969f663b81ca13c75e3da8bbb8130015db1dd48c0fad77d0afed559e8d21d1a4e0f58abf7ea7cd63f01f547ce9f3537276dd5bb20c762d574942a4f97ac1ab2f5bee55208a56666392727a7e55aa770494744714a2e2f5fe9771ca729bbc9ddd92f921b453024028a000028a000028a000028a0000e6b5ed7355b1bb78e09630a151951e3460415e99c67a83cd41ae3c5737ff290db2208d8e8486cfe99c563528d269dd7e2cc6b578fb4b277b2d4f47098dc5c795c6764b45eec7a74db547a196e595bea8a738b8f3cf9a375adadfadae717a8f8e35f8dda38ee25b16c9e52c16ee361ea1d19641f88e2ba808abd005e3a818e2b96741c5fbaefe4d3fce37fc8e8fac247a9431d4ea413ab0707d654e51b7fe0151c5af94d9c6f2b6fa36725e1ed4b58d6350cdf6a7f69470a85a5b0fb3c7101d18bafef09c7186e3d4d6beb8b7173672c56d2c7048dc0790b6cc13f367690dcae4023919ae1797bc54b56a9ff00795dbfbac91d553137564ec7bb478a61925292a71a98b4f55467c94d45f56a6a7525f24ac79786ca391f338737c8bdad7c37b7d6c077f25d881b6789fcb765ed93b1d587a673ed5cde9fe208fc291ac697e026ed8b6f1bcb71b9fd36bee193d80c9ae459463293fddd5a525e7ccbf0b3fc19d30c4ba7b3f97fc39ebd5e3ae1ec7c7fdaf018ea3516ee92a3297a73f3c2ff00f6f44f2f139452c47f1611bff368a5f2e5d4ed7c21e01b1f0b132fcb34fced38c84cff0016e20177c7009002f614fb5f155ecb0c72c9681030c94903472e3d48dcc17239c119f515a6132e74a6aad79aa9517c2a37508fa5f765ac73ddc51c39df15431742782cb684b0b849ff0016555c6588afe5371f7630feea6efdcce5c3306da8d5a89f4d135fa3fc4eb7248c12715434cd560d523668f2ac870f19232b9e878ea0fad76597633a35e15d371dd6e8f9d726d59b6d2e973b332caf11964e31aaaf19a6e1357b4adbaf26bb17a8ad40e200a280000a280000a280000a2800031b5ad6fec04410806775dd96fbb1a9e848eec7b0e9eb5a725a5bcadbe482276e9b9a356381ee466bcfcc3325856a9c2cea357d768aff003ec764f0f46a3e69d2a727de508b7f7b47d5709f084f3d8cb195dca38484dc128e93ad38d9b8a7f660aeb99ef7d11e050cd330c2d354a863315469a6da852af5610bbddf2c6495df53847ba9a42cd24d248cddd989e7d8741f85773f61b4ff009f683fefd27f857cdcb1b3936e536dbeecfa2faa61ff00e7cd2ffc023fe47ebb4f87a8518c614b0d4e9c63b28c12fbddaefe67e4dfdb99b7fd0c71dff8555bff00933848988c9eb9239aee5f4eb2931bada1e3d102ff00e838af9c5897dcfa196070b3de853f9452fcac7ebb53288e8b9765d8fc9e8712677866dd3ccb18afbf35694d7dd53991e79ae6a2d6b6f88ced760d86f4c0249fafa577e745d31befd95b3f047ef230fc1ebf7f775af01e25bb23df8603090d63429fdd7fcee7ea71ca1538ca6e2badb4ecae7e5b88e26cf315151ab9962e514ef6553915ff00ee1f29f2d5d6bbab6a53187ed333ef6202a9c679f6afa6d3c29e1e88ee8f47d3636ce7725a428d9ff795437eb5e4d9455d9eccb0d4271e5952835dac7d94ab57af53d9c5b6dbb25156fc8f85a19be6386a8aad2c66221516d2551dff001ba3c9fc15e115b1097d7bfbdb8c7eec3722107bae7f88f73d6bd8bfb1b4fc63ecea07a0671fc9abe7a5894de9b2fc4f69e55827ff002e52f494d7fedc7ea987c925469a7535ab25b7f227d3d5f53f3d8f1af11475fed09c9f79d2a12fce99e7dab6a1e4a10bd6bb86f0be8ce72f66ae7fdb7948fcb7e2bc278972764cf7639560a3b515f394dfe723f4c86511a51e79475e87e695b8df88ebab4b309a5fdda5423f8c69a673bf0eace7f2ef2fe52db67611479fe20872cdf407007e35dadbdbc3691243046b14518c246830aa339e07d6b0ca6126a755ecfdd5e76d5bfd0f46108d38a8c528c56c96c8f438eabc213c360d59d48deb54feea92e5847d5abcbd2c7c9d7c456c5559d6af52556acdde539bbca4ed6d5fa1251540660145000014500007ffd9"; public static byte[] imagebytes; public static void main(string[] args) { //run conversion function (convert hex values in string byte integers) imagebytes = hexstringtobytearray( image_hexstr ); system.out.println("byte value 0 : " + imagebytes[0] ); system.out.println("byte value 1 : " + imagebytes[1] ); system.out.println("byte value 2 : " + imagebytes[2] ); system.out.println("byte value 3 : " + imagebytes[3] ); } public static byte[] hexstringtobytearray(string input) { return datatypeconverter.parsehexbinary( input ); } }
Comments
Post a Comment