Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to convert image into Hex 2 Dimensional Array?

Reply
Thread Tools

How to convert image into Hex 2 Dimensional Array?

 
 
sout saret
Guest
Posts: n/a
 
      05-08-2013
Dear Community!

May you help me how to write code in java to convert image to Hex 2 dimensional array. I want to show format as below:

{
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1B,0xFB ,0x12,0x0A,0x1B,0x0B,0x09,0x09,0x19,0x09,0x19,0x29 ,0x19,0xE9,0x00,0x20,0x00,0x60,0x00,0x00,0x00,0x00 ,0x00,0x00,},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6D,0xFD ,0x48,0x05,0x6D,0xF5,0x25,0x14,0x25,0x15,0x25,0x15 ,0xFD,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,},
{0x00,0xC0,0x01,0x00,0x01,0xC0,0x00,0x00,0xBC,0x78 ,0x24,0x48,0xA4,0x48,0xA4,0x48,0xB4,0x48,0xB5,0x48 ,0xB6,0xC8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,},
};

Best Regard!

Saret Sout
 
Reply With Quote
 
 
 
 
Joerg Meier
Guest
Posts: n/a
 
      05-08-2013
On Wed, 08 May 2013 09:06:09 +0100, lipska the kat wrote:

> On 08/05/13 07:30, sout saret wrote:
>> May you help me how to write code in java to convert image to Hex 2 dimensional array. I want to show format as below:

> [snip]


> hmm, sounds like homework.


It sounded more like he wanted to hardcode an image into his class files,
which is a fantastically terrible idea that's usually attempted by people
who decided that loading resources in jar files is too complicated to
figure out, which is why I stayed away

Liebe Gruesse,
Joerg

--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
 
Reply With Quote
 
 
 
 
Daniele Futtorovic
Guest
Posts: n/a
 
      05-08-2013
On 08/05/2013 10:06, lipska the kat allegedly wrote:
> Byte[][] bytes = new Byte[width][height];


Lipska, mind the difference between Byte and byte. That code above is
extremely bad advice.

--
DF.
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      05-08-2013
On 05/08/2013 02:30 AM, sout saret wrote:
> Dear Community!
>
> May you help me how to write code in java
> to convert image to Hex 2 dimensional array.
> I want to show format as below:
>

You've asked too much for a simple usenet post.
You might read the article here:
<http://www.developer.com/java/other/article.php/3403921>
It's a pretty long article but at the end you should
understand how to get the image data and then you will
only need to figure out how to represent it as a hex string
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      05-08-2013
On 5/8/13 6:57 AM, lipska the kat wrote:
> On 08/05/13 14:19, Daniele Futtorovic wrote:
>> On 08/05/2013 10:06, lipska the kat allegedly wrote:
>>> Byte[][] bytes = new Byte[width][height];

>>
>> Lipska, mind the difference between Byte and byte. That code above is
>> extremely bad advice.

>
> Why? it's common practice these days to eschew primitives and use
> (autoboxing) classes

Common practice where? Cite a credible source or I'll be forced to think
that you are just making up facts to support your mistake. My 4 year
old daughter does that; you want to be more credible than my 4 year old,
don't you?

> it's more consistent in OO code and, as I
> understand things, post autoboxing, the following two lines are
> logically equivalent.
>
> 1. Byte[][] bytes = new Byte[width][height];
> 2. byte[][] bytes = new byte[width][length]


Even ignoring the typos, those are nothing alike.

Auto-boxing is a convenience, but does not make those two things the
same, even by a long shot.

byte cannot contain a NULL reference. It contains no reference
whatsoever. Byte on the other hand *can* contain a reference.

byte[] is *not* assignable to Byte[], and Byte[] is *not* assignable to
byte[].

You also cannot use arrayCopy to copy between the two.

Boxing and Unboxing (whether manual or automatic) *does* have a cost. It
may be minimal, and shouldn't be the only consideration. It should
however be noted and considered as part of the choice.

Personally my decision on whether to use Byte vs byte would hinge solely
on the question "do I want to store nulls in this?"

Also, I would likely wrap that primitive value in a semantic class. I
still lament that Java doesn't have "value types" other than the
predefined primitives, but I have learned to be "okay" with that.
 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      05-08-2013
On 08/05/2013 15:57, lipska the kat allegedly wrote:
> On 08/05/13 14:19, Daniele Futtorovic wrote:
>> On 08/05/2013 10:06, lipska the kat allegedly wrote:
>>> Byte[][] bytes = new Byte[width][height];

>>
>> Lipska, mind the difference between Byte and byte. That code above is
>> extremely bad advice.

>
> Why? it's common practice these days to eschew primitives and use
> (autoboxing) classes, it's more consistent in OO code and, as I
> understand things, post autoboxing, the following two lines are
> logically equivalent.
>
> 1. Byte[][] bytes = new Byte[width][height];
> 2. byte[][] bytes = new byte[width][length]
>
> The following is perfectly legal given either of the above
>
> byte b = 127;
> bytes[0][0] = b;
> Byte c = 126;
> bytes[0][1] = c;
> Byte d = bytes[0][0];
> byte e = bytes[0][1];
>
> I would of course be interested to know what your objection is.


Simple: "byte[]" is not a primitive type. "byte" is a primitive type.
"byte[]" is a reference type.

Along the same lines, you'll be hard pressed to find a Java I/O library
that uses Byte[]'s rather than byte[]'s.

> Also, I seem to remember being 'told off' for using primitives a while
> back ... ho hum.


I don't know about that.

--
DF.
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      05-08-2013
On 05/08/2013 04:06 AM, lipska the kat wrote:
> On 08/05/13 07:30, sout saret wrote:
>> Dear Community!
>>
>> May you help me how to write code in java to convert image to Hex 2
>> dimensional array. I want to show format as below:

>
> [snip]
>
> hmm, sounds like homework.


hmm. Sounds like the beginning of another snarky reply.

>
> 1. get hold of the javadoc for your chosen Java version
>
> Then ...
>
> To get you started.
>
> import javax.swing.ImageIcon;
>
> public class ImageToHex {
>
> public static void main(String[] args) {
>
> ImageIcon ii = new ImageIcon("/path/to/image.ext");
> Integer width = ii.getIconWidth();
> Integer height = ii.getIconHeight();
> Byte[][] bytes = new Byte[width][height];
> //you now have 2 dimensional array into which
> //you can read your image bytes
> //the rest is up to you I guess
> }
> }
>
> I'm sure there are other/better ways of doing it
>


WTF Why bother?
 
Reply With Quote
 
Joshua Cranmer 🐧
Guest
Posts: n/a
 
      05-08-2013
On 5/8/2013 8:57 AM, lipska the kat wrote:
> Why? it's common practice these days to eschew primitives and use
> (autoboxing) classes, it's more consistent in OO code and, as I
> understand things, post autoboxing, the following two lines are
> logically equivalent.
>
> 1. Byte[][] bytes = new Byte[width][height];


This creates an array with every entry prefilled to NULL.
> 2. byte[][] bytes = new byte[width][length]


This creates an array with every entry prefilled to 0.

Ergo, not logically equivalent, even if I assume that your height/length
was a typo.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      05-08-2013
On 08/05/2013 18:17, lipska the kat allegedly wrote:
> On 08/05/13 16:32, Daniele Futtorovic wrote:
>> On 08/05/2013 15:57, lipska the kat allegedly wrote:
>>> On 08/05/13 14:19, Daniele Futtorovic wrote:
>>>> On 08/05/2013 10:06, lipska the kat allegedly wrote:
>>>>> Byte[][] bytes = new Byte[width][height];
>>>>
>>>> Lipska, mind the difference between Byte and byte. That code above is
>>>> extremely bad advice.
>>>
>>> Why? it's common practice these days to eschew primitives and use
>>> (autoboxing) classes, it's more consistent in OO code and, as I
>>> understand things, post autoboxing, the following two lines are
>>> logically equivalent.
>>>
>>> 1. Byte[][] bytes = new Byte[width][height];
>>> 2. byte[][] bytes = new byte[width][length]

>
> typo notwithstanding oops
>
>>> The following is perfectly legal given either of the above
>>>
>>> byte b = 127;
>>> bytes[0][0] = b;
>>> Byte c = 126;
>>> bytes[0][1] = c;
>>> Byte d = bytes[0][0];
>>> byte e = bytes[0][1];
>>>
>>> I would of course be interested to know what your objection is.

>>
>> Simple: "byte[]" is not a primitive type.

>
> I wasn't aware that I said it was


When I called out your using a Byte[] rather than byte[] in your example
code, you defended it on the grounds of an alleged "common practice" to
use (boxed) reference types instead of primitive types. Thereby, you
have implied that byte[] was a primitive type; otherwise your whole
argument would have been a non sequitur.

But byte[] is not a primitive type, and hence your argument,
irrespectively of its merits (see below), does not apply.

>> "byte" is a primitive type.
>> "byte[]" is a reference type.

>
> no argument there but what is your *objection* to
>
> Byte[][] bytes = new Byte[width][height];
>
> in the context of the OPs question?


Objection? It's bullcrap. Firstly, no I/O library uses Byte[]. Secondly,
as Daniel has indicated, the only reason to use Byte[] over byte[] would
be to store null references. Which the OP doesn't need, as they have
actual data. And lastly, it's dangerous because it furthers the very
thing that, I reckon, lead you to use it in the first place: the
confusion induced by autoboxing.

General OO matters set aside, in the context of Java, autoboxing is a
poison. The meagre convenience it provides is largely offset by the
confusion, or lack of clarity, it causes in the minds of Java
programmers, especially aspirant Java programmers. I've witnessed its
nefarious effects countless times in my colleagues' code. A for/each
loop is exactly the same thing as a normal for loop with an iterator.
But, for better or for worse, in Java, a boxed reference type and a
primitive type are two completely different things. Autoboxing induces
the thought that they're not. That's why it's poison.

--
DF.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-08-2013
On 5/8/2013 11:22 AM, Daniel Pitts wrote:
> On 5/8/13 6:57 AM, lipska the kat wrote:
>> On 08/05/13 14:19, Daniele Futtorovic wrote:
>>> On 08/05/2013 10:06, lipska the kat allegedly wrote:
>>>> Byte[][] bytes = new Byte[width][height];
>>>
>>> Lipska, mind the difference between Byte and byte. That code above is
>>> extremely bad advice.

>>
>> Why? it's common practice these days to eschew primitives and use
>> (autoboxing) classes

> Common practice where? Cite a credible source or I'll be forced to think
> that you are just making up facts to support your mistake. My 4 year
> old daughter does that; you want to be more credible than my 4 year old,
> don't you?
>
>> it's more consistent in OO code and, as I
>> understand things, post autoboxing, the following two lines are
>> logically equivalent.
>>
>> 1. Byte[][] bytes = new Byte[width][height];
>> 2. byte[][] bytes = new byte[width][length]

>
> Even ignoring the typos, those are nothing alike.
>
> Auto-boxing is a convenience, but does not make those two things the
> same, even by a long shot.


For example, consider what happens when `int x = bytes[0][0];'
immediately follows either of the above. In case 2, it sets x
to zero. In case 1, the JVM throws NullPointerException. That's
not "logically equivalent" in my book!

>[...]
> Personally my decision on whether to use Byte vs byte would hinge solely
> on the question "do I want to store nulls in this?"


Or on "Do I want to put it in a Collection?"

There's also the matter of the memory footprint, even if
"efficiency" is a dirty word in some circles. Version 1 will
use four times (32-bit JVM) or eight times (64-bit) as much
memory as version 2. Image sizes vary, but even a fairly modest
640x480 has 300K pixels -- so using Byte[][] instead of byte[][]
would cost an extra 0.9MB or 2.1MB. Even if I had the desire to
store nulls, a bloat of such proportions would probably prompt
me to use byte[][] and seek another way to represent whatever
I wanted the nulls to stand for.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
How to convert an hex string to a Hex number chirs Javascript 3 12-01-2003 10:06 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments