2017-01-09 7 views
5

私はバイト[]配列として格納されている画像を持ち、バイトをディスクの他の場所に書き込む前に画像を垂直に反転します。バイト配列として格納された画像を反転する

イメージバイトは、圧縮されたjp2イメージファイルからのものです。 Flip image stored as a byte[] arrayのようなものを実装してみましたが、アンドロイドで作業していないため、BitmapFactoryにアクセスできません。私はまた、最初にBufferedImageにバイト配列を変換し、それを反転させることを検討しましたが、イメージの高さと幅は現在のコンテキストではわかりません(EDIT:コードを変更して、現在知られている)。

厳密な配列操作でこれを行う方法はありますか?

EDIT:未遂フリップコード

public static byte[] flip(byte[] imageBytes) { 
    //separate out the sub arrays 
    byte[] holder = new byte[imageBytes.length]; 
    byte[] subArray = new byte[dimWidth];//dimWidth is the image width, or number of matrix columns 
    int subCount = 0; 
    for (int i = 0; i < imageBytes.length; i++) { 
     subArray[subCount] = imageBytes[i]; 
     subCount++; 
     if (i% dimWidth == 0) { 
      subArray = reverse(subArray); 
      if (i == (dimWidth)) { 
       holder = subArray; 
      } else { 
       holder = concat(holder, subArray); 
      } 
      subCount = 0; 
      subArray = new byte[dimWidth]; 
     } 
    } 
    subArray = new byte[dimWidth]; 
    System.arraycopy(imageBytes, imageBytes.length - dimWidth, subArray, 0, subArray.length); 
    holder = concat(holder, subArray); 
    imageBytes = holder; 
    return imageBytes; 
} 
+0

それでは、わかっていることはありますか? –

+0

@HovercraftFullOfEels厳密に言うと、インターリーブする前に、自分の配列に高位バイトと低位バイトがあります。かなり知られているのは、ピクセルデータ配列と元の圧縮前ヘッダー情報を含むデータ配列です。 – Sarah

+1

"画像の高さと幅は分かっていません"とすると、ピクセルを入れ替える場所を知ることができなくなります(高さと幅が素数で、どちらが大きければ大きいかわかりません)。 –

答えて

1

ネヴァーマインドの連中は、私が働いてそれを得ました。私はちょうど最後の配列にそれらをインタリーブする前に、高および低バイトを反転しなければならなかった。私と同じ問題を抱えている人には、インターリーブする前に、上位と下位のバイト配列で上記のフリップ関数を別々に使用してください。

ありがとうございました!

+0

は自分自身の問題を解決するためにプラス1をアップしました。 –

0

Iはまた、それを反転そして、第1 BufferedImage にバイト配列を変換するに見てきたが、画像のは、現在のコンテキストで公知 ありません。

あなただけJP2ファイルのバイトのヘッダ部から直接、高さ&幅をチェックすることができます。

import java.io.IOException; 
import java.io.FileInputStream; 
import java.io.File; 

import java.nio.ByteBuffer; 
import javax.xml.bind.DatatypeConverter; 


public class parse_Header_JP2 
{ 
    static File  myFile;  static FileInputStream fileInStream = null; 
    static byte[] myBytes; static String   myString = ""; 

    static int myNum = 0; static int myWidth = 0; static int myHeight = 0; 
    static ByteBuffer byteBuff; 

    public static void main(String[] args) 
    { 
     myFile = new File("c:/test/image.jp2"); 
     myBytes = getBytes_Header_JP2(myFile); 
     checkBytes_Header_JP2(myBytes); //# update myWidth & myHeight 

     //# Shows HEX of whole bytearray 
     System.out.println("First 64 bytes (as HEX) : \n" + bytesToHex(myBytes)); 
    } 

    private static byte[] getBytes_Header_JP2(File file) 
    { 
     myBytes = new byte[64]; //will hold first 64 bytes of file as header bytes 

     try 
     { 
      //# convert file into array of bytes 
      fileInStream = new FileInputStream(file); 
      fileInStream.read(myBytes, 0, 64); //# Read only first 64 bytes 
      fileInStream.close(); 
     } 
     catch (Exception e) { e.printStackTrace(); } //# error catching 

     byteBuff = ByteBuffer.wrap(myBytes); //associate ByteBuffer with bytes 

     return myBytes; 
    } 

    public static void checkBytes_Header_JP2(byte[] bytes) 
    { 
     int offset = 0; myHeight = 0; myWidth = 0; // resets 

     while(true) 
     { 
      //# set as byte value reading from offset 
      myNum = bytes[offset]; myString = Integer.toHexString(myNum).toUpperCase(); 

      //# Check byte as : Hex value 
      System.out.println("Byte Value at [" + offset + "] : " + decimalToHex(myNum)); 

      //# Check byte as : Decimal value 
      //System.out.println("Byte Value at [" + offset + "] : " + myNum); 

      //# Find byte 0x69 (or Decimal = 105) which is letter "i" from "ihdr" 
      if (myNum == 0x69) //# if "i" is found at this offset within bytes 
      { 
       //# From this offset check if reading 4 bytes gives "ihdr" (as bytes 69-68-64-72) 
       if (byteBuff.getInt(offset) == 0x69686472) //# if the 4 bytes make "ihdr" 
       { 
        System.out.println("found \"ihdr\" section at offset : " + offset); 

        //# extract Width or Height from this offset 
        myHeight = byteBuff.getInt(offset+4); //# +4 from "ihdr" is Height entry 
        myWidth = byteBuff.getInt(offset+8); //# +8 from "ihdr" is Width entry 

        //# check values 
        System.out.println("Image Width : " + myWidth); 
        System.out.println("Image Height : " + myHeight); 

        break; //# end the While loop (otherwise runs forever) 
       }  
      }  

      offset++; //# increment offset during While loop process 
     }  
    } 

    //# Convert byte values to Hex string for checking 
    private static String bytesToHex(byte[] bytes) 
    { myString = ""; myString = DatatypeConverter.printHexBinary(bytes); return myString; } 

    private static int bytesToNumber(ByteBuffer input) //throws IOException 
    { input.rewind(); myNum = input.getInt(); return myNum; } 

    private static String decimalToHex(int input) 
    { 
     input = input & 0xFF; myString = Integer.toHexString(input); 
     if(myString.length() == 1) {myString="0"+myString;} 
     return myString.toUpperCase(); 
    } 
} //end Class 
関連する問題