which one is best method to read image and convert it into byte array in java and opencv

asked 2018-06-10 01:09:10 -0600

vish gravatar image

updated 2018-06-10 01:24:00 -0600

berak gravatar image

I'm trying to generate byte array from image for that i tried two different method but i'm not understanding which one is better because both method require so much time

so if any alternative available please suggest

Java code :

    File newfile = new File(path);
    BufferedImage originalImage = ImageIO.read(newfile);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(originalImage, "png", baos);
    imageInByte = baos.toByteArray();

Opencv method which i tried :

Mat originalImage=  Highgui.imread(path);
byte[] imageInByte = new byte[(int) (originalImage.total() * originalImage.channels())];
originalImage.get(0, 0, imageInByte);
System.out.println("CONVERTED TO BYTE");
edit retag flag offensive close merge delete

Comments

WHY do you you need a byte array ? (without any context, it's an xy problem)

and both methods don't do the same thing. the former is producing a png representation in memory, the latter returns pixels only.

berak gravatar imageberak ( 2018-06-10 01:12:52 -0600 )edit

i need byte array for processing the image.how should i go about it?

vish gravatar imagevish ( 2018-06-10 01:54:04 -0600 )edit

i need byte array for processing the image

not an answer to my question.

which problem are you trying to solve here ?

and you probably should NOT do any processing on a per-pixel level, but use opencv or the like.

berak gravatar imageberak ( 2018-06-10 01:58:18 -0600 )edit

Its my assignment.where i have to perform encryption at pixel level.

vish gravatar imagevish ( 2018-06-10 02:28:11 -0600 )edit

public class POB_enc {

public String[] PaddingOne(String[] ss) {
    String Tokn = "", Temp = "";
                    for (int i = 0; i < ss.length; i++) 
    {
        int cnt1 = 0;
        int k =0;
        Tokn = ss[i]; //String.format("%8s", Integer.toBinaryString(ss[i] & 0xFF)).replace(' ', '0');
        int result = 0;
        for (int j = Tokn.length() - 1 ; j >= 0; j--,k++) {
            if ('1' == Tokn.charAt(j)) {
                cnt1 = cnt1 + 1;
                if ( k >= cnt1 || cnt1 >= 0) {
                //  System.out.println(Tokn.length());
                    int m = (factorial(k) / (factorial(k - cnt1) * factorial(cnt1)));
                        result += m;
                    Temp = Temp + "1";
                } else {
                    Temp = Temp + "0";
                }
            }ss[i] = ""+result;         
    }
    System.out.println("-" + Arrays.toString(ss));
    return ss;
}
vish gravatar imagevish ( 2018-06-10 02:32:24 -0600 )edit

ah, ok (that was the answer....)

so, read up the docs on ImageIO.write again, its probably not what you want or need.

berak gravatar imageberak ( 2018-06-10 02:33:56 -0600 )edit

so cant i use opencv for reading pixel from image and reconstructing image in this case

vish gravatar imagevish ( 2018-06-10 03:08:35 -0600 )edit

what ? it's the other way round. if you would try with ImgIO.read(), you would have to decode the image first

berak gravatar imageberak ( 2018-06-10 03:10:44 -0600 )edit

please clarify, if you want to do this at file or at pixel level.

berak gravatar imageberak ( 2018-06-10 03:11:59 -0600 )edit

i want to run the above code on every pixel

vish gravatar imagevish ( 2018-06-10 03:16:44 -0600 )edit