reading a image into a int array
how can i do this
i know how to read the image into a byte array
but not int array
how can i do this
i know how to read the image into a byte array
but not int array
Uh, just taking a wild guess -- you create an array of int and repeat whatever you did before? ;)
If that's not what you wanted to do, you'll have to be a bit more specific in your request.
> Uh, just taking a wild guess -- you create an array
> of int and repeat whatever you did before? ;)
>
> If that's not what you wanted to do, you'll have to
> be a bit more specific in your request.
images can be read as stream of bytes .they cant be stored into an integer array.
> how can i do this
> i know how to read the image into a byte array
> but not int array
need to understand basics of computing first ..!
> how can i do this
> i know how to read the image into a byte array
> but not int array
try searching the forums, this question has been asked and code has been posted, i'm sure more than a few times
> > Uh, just taking a wild guess -- you create an
> array
> > of int and repeat whatever you did before? ;)
> >
> > If that's not what you wanted to do, you'll have
> to
> > be a bit more specific in your request.
>
> images can be read as stream of bytes .they cant be
> stored into an integer array.
An array of int seems to be more correct other than using an array of Integer objects. Anyway you only need an array of int if your image is bigger than 4 byte, these because an int in java has 4 byte.
MeTitus
> Four bytes in an int?! Why wasn't I informed!?
You can inform yourself right here.
http://mindprod.com/jgloss/conversion.html
MeTitus
I am not entirelly sure why you need to do this, but here is something that might help you.
byte[] byteImage = new byte[8];
int[] intImage = new int[byteImage.length/4];
byteImage[0] = 1;
byteImage[1] = 2;
byteImage[2] = 3;
byteImage[3] = 4;
byteImage[4] = 5;
byteImage[5] = 6;
byteImage[6] = 7;
byteImage[7] = 8;
String strTmp = "";
for(int iter=0;iter<byteImage.length;iter++)
{
strTmp += byteImage[iter];
strTmp += byteImage[++iter];
strTmp += byteImage[++iter];
strTmp += byteImage[++iter];
intImage[(iter/4)] = Integer.parseInt(strTmp);
strTmp = "";
}
MeTitus>
What?
Basically a byte can hold 256 characters, in digits this can go from 0 to 255.
255 = 111111111
0= 00000000
MeTitus
> What if:
> > byteImage[0] = (byte)255;
> ...
> byteImage[7] = (byte)255;
>
And you don't need to cast.
byteImage[0] = 255;
byteImage[7] = 255;
MeTitus
> Basically a byte can hold 256 characters
What do you mean, 256 characters. Like a string of length 256?
> And you don't need to cast.
>
> byteImage[0] = 255;
>
> byteImage[7] = 255;
But with this example:
public class Konfused {
public static void main(String[] args) {
byte[] byteImage = new byte[8];
byteImage[0] = 255; //error on this line
}
}
I am getting the compile-time error: possible loss of precision
A byte can hold the ASCII characters from 0 to 255 if note unsegned
or -127 to 128 if I am not mistaken cause the last byte value would hold the sign value.
MeTitus
> A byte can hold the ASCII characters from 0 to 255 if note unsegned
How do you make a byte unsigned in Java?
To get this easier, you can have 256 different combinations in a byte, if you have 2 then its 256*256. Some byte values have an ASCII correspondent other dont, for eg, chinese characters need to be represented by e byte UTF16, 2*8 bit, because they don't have an correspondent in the ASCII table. Don't think about int as number or strings as letters, they are all bytes anyway. But constraints were added to int, long... primitive types which say that they can only hold bytes which have an numeric match in the ASCII table.
Hope this helps.
MeTitus
> > or -127 to 128 if I am not mistaken
>
> Still getting the error "loss of precision"
> > byte b = 128;
>
Just shift 8 bits. You are not going to loose any precision anyway.
<< 8
MeTitus
>. But constraints were added to
> int, long... primitive types which say that they can
> only hold bytes which have an numeric match in the
> ASCII table.
What in the ?
Do you really need to drag ASCII into the description of datatype byte? Is that the only way to define a byte? How does this related to the original question -- placing image data into an int[]? What is the relationship of ASCII tables to image data?
> Just shift 8 bits. You are not going to loose any
> precision anyway.
>
> << 8
>
> MeTitus
When I shift 8 bits:
byte b = 1 << 8;
I still get the error "loss of precision".
> byte b = (byte) 128;
In java a byte is sign, so like I said before it uses the last position of the byte to
represent the sign.
It seems you need to cast anyway.
byte a = (byte)127 -- valid
byte c = (byte)256 --not valid
MeTitus
> > A byte can hold the ASCII characters from 0 to 255
> if note unsegned
>
> How do you make a byte unsigned in Java?
byte a = -127;
MeTitus
> >
> byte c = (byte)256 --not valid
>
>
>
You know that compiles right?
And could you explain what the hell you meant when you started taking about constraints on primitives and ASCII tables?
That's for your explanation. But your code for converting 4 bytes into an int still isn't working:
public class Konfused {
public static void main(String[] args) {
byte[] byteImage = new byte[8];
int[] intImage = new int[byteImage.length/4];
byteImage[0] = (byte)255;
byteImage[1] = (byte)255;
byteImage[2] = (byte)255;
byteImage[3] = (byte)255;
byteImage[4] = (byte)255;
byteImage[5] = (byte)255;
byteImage[6] = (byte)255;
byteImage[7] = (byte)255;
String strTmp = "";
for(int iter=0;iter < byteImage.length;iter++) {
strTmp += byteImage[iter];
strTmp += byteImage[++iter];
strTmp += byteImage[++iter];
strTmp += byteImage[++iter];
intImage[(iter/4)] = Integer.parseInt(strTmp);
strTmp = "";
}
}
}
When I run this, I get the error "java.lang.NumberFormatException: For input string: "-1-1-1-1".
The error is thrown by parseInt.
> Basically a byte can hold 256 characters, in digits
> this can go from 0 to 255.
> 255 = 111111111
> 0= 00000000
> MeTitus
That is true for the make believe world of unsigned types which
java doesnt have (except for char).
In the signed world the order of the values is
0 -> 127 then -128 -> -1
so the conversion is:
byte b = (number > 127) ? (number - 256) : number;
public class ByteOperations{
public static void main(String[] args){
byte b;
b = convertUnsigned(0);
b = convertUnsigned(127);
b = convertUnsigned(128);
b = convertUnsigned(255);
}
public static void display(byte b){
int num = Integer.parseInt("FF", 16) & (int)b;
System.out.println("Byte " + b + ": " + Integer.toBinaryString(num));
}
public static byte convertUnsigned(int num){
byte b = (num > 127) ? (byte)(num - 256) : (byte)num;
display(b);
return b;
}
}
> > Just shift 8 bits. You are not going to loose any
> > precision anyway.
> >
> > << 8
> >
> > MeTitus
>
> When I shift 8 bits:
> > byte b = 1 << 8;
>
> I still get the error "loss of precision".
Why are you shifting there, makes no sense. Eg:
byte[] aa = new byte[2];
int aInt = 300; // 2 bytes
aa[0] = (byte)aInt;
aInt = aInt >> 8;
aa[1] = (byte)aInt;
MeTitus
> > > >
>
> > byte c = (byte)256 --not valid
> >
> >
> >
>
> You know that compiles right?
>
> And could you explain what the hell you meant when
> you started taking about constraints on primitives
> and ASCII tables?
I didnt explain myself right. I wasn't saying that constrainst were added to the ascii table, but that primitive types such as short, int, long... can only have the byte values that have a digit match in the ascii table.
MeTitus
> TP: thanks for clarifying things. Once you see it,
> it's so simple.
Good we could help you, and just to finalize.
the maximum number an int can if, is givem by this:
255 255 255 255 = 4 (byte * 255)
MeTitus
> the maximum number an int can if, is givem by this:
>
> 255 255 255 255 = 4 (byte * 255)
or rather:
-1 -1 -1 -1
Titus' code doesnt work because he's trying to use Integer.parseInt
with a makeshift string of negatives, lol.
Integer.parseInt("-1-1-1-1", 16);
If you are going the String way (which is a HORRIBLE idea) at least
do it right. Output the integer as a Hex String and then do String
processing to assure that the String is padded with a 0 in front and
is exactly 2 characters.
> > > -4?
> >
> > o_O
> >
> >
> > MeTitus
>
> Reply #27 -- your code isn't working.
lol, I did that with notepad, in my sisters computer couldn't get right at first...
It is not going to work if you have even numbers... I'll fix that later..
But anyway the code was just to help the op understand about bits and bytes ;)
MeTItus
> Titus' code doesnt work because he's trying to use
> Integer.parseInt
> with a makeshift string of negatives, lol.
>
> Integer.parseInt("-1-1-1-1", 16);
>
> If you are going the String way (which is a HORRIBLE
Thats why we have people like you that help the community improving ;)
We can't be all like you...
How would you do that then? Trying to improve here ;)
MeTitus