java - Converting image to byte array, then to int array, then to byte array for image output -
i'm trying implement dct on image, , far have been able read image, grayscale it, turn byte array , output byte array image. works fine. however, in order work on image need convert byte array int array , again, , problem comes in.
this first part reads image , converts image grayscale.
bufferedimage image = imageio.read(new file("c:\\users\\a00226084\\desktop\\image.jpg")); int width = image.getwidth(); int height = image.getheight(); for(int i=0; i<height; i++){ for(int j=0; j<width; j++){ color c = new color(image.getrgb(j, i)); int red = (int)(c.getred() * 0.299); int green = (int)(c.getgreen() * 0.587); int blue = (int)(c.getblue() *0.114); color newcolor = new color(red+green+blue, red+green+blue,red+green+blue); image.setrgb(j,i,newcolor.getrgb()); } }
this part converts image byte array.
bytearrayoutputstream baos = new bytearrayoutputstream(); imageio.write(image, "jpg", baos); byte[] pixels = baos.tobytearray();
this part converts byte array int array. have manually done wrapping because nothing else worked.
for(int = 0; < pixels.length; ++){ if(pixels[i] < 0){ byteconverted[i] = 127 + (128 - (pixels[i] * -1)); }else{ byteconverted[i] = pixels[i]; } }
this part converts int array byte array, somewhere between byte > int , int > byte conversions goes wrong. have output both before , after byte array file , identical, don't know why 'image == null' instead of image.
for(int = 0; < pixels.length; ++){ if(byteconverted[i] > 127){ pixels[i] = (byte) ( -128 + (byteconverted[i] - 127)); }else{ pixels[i] = (byte) byteconverted[i]; } } write2("final byte array.txt", pixels); bytearrayinputstream bais = new bytearrayinputstream(pixels); try { bufferedimage img = imageio.read(bais); system.out.println("image out"); system.out.println(pixels.length); imageio.write(img, "jpg", new file("c:\\users\\a00226084\\desktop\\newimage.jpg")); } catch (ioexception e) { e.printstacktrace(); }
Comments
Post a Comment