2017-01-09 7 views
5

मैं एक छवि है जो एक बाइट [] सरणी के रूप में संग्रहीत किया जाता है, और मुझे खड़ी कहीं और डिस्क पर बाइट्स लिखने से पहले छवि फ्लिप करना चाहते हैं।एक बाइट सरणी के रूप में जमा एक छवि फ्लिप

छवि एक संकुचित JP2 छवि फ़ाइल से आते हैं बाइट्स। मैंने Flip image stored as a byte[] array जैसे कुछ को लागू करने में देखा है, लेकिन मैं एंड्रॉइड में काम नहीं कर रहा हूं और बिटमैप फैक्ट्री तक पहुंच नहीं है। मैं भी एक BufferedImage के लिए बाइट सरणी परिवर्तित पहले, तो यह flipping में देखा है, लेकिन ऊंचाई और छवि की चौड़ाई वर्तमान संदर्भ (संपादित में ज्ञात नहीं है: मैं कोड को संशोधित किया है ताकि ऊंचाई और चौड़ाई हैं अब जाना जाता है)।

सिर्फ सख्त सरणी हेरफेर के साथ ऐसा करने के लिए एक रास्ता है?

संपादित करें: का प्रयास किया गया फ्लिप कोड

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

@ होवरक्राफ्टफुलऑफेल सख्ती से बोलते हुए, मेरे पास इन्हें इंटरलविंग करने से पहले अपने स्वयं के सरणी में उच्च बाइट्स और कम बाइट्स हैं। जो कुछ भी जाना जाता है वह पिक्सेल डेटा सरणी और मूल पूर्व-संपीड़न शीर्षलेख जानकारी वाले डेटा सरणी है। – Sarah

+1

"लेकिन ऊंचाई और छवि की चौड़ाई ज्ञात नहीं है" यह यह असंभव को पता है जहां के साथ पिक्सल स्वैप करने के लिए बनाता है (जब तक ऊंचाई और चौड़ाई प्रधानमंत्री हैं, और आप जानते हैं कि कौन बड़ा है)। –

उत्तर

1

कोई बात नहीं दोस्तों, मैं यह काम कर रहा मिला है। मुझे अंतिम सरणी में उन्हें अंतःस्थापित करने से पहले उच्च और निम्न बाइट्स को फ़्लिप करना पड़ा। मेरे जैसा ही समस्या है, उपरोक्त फ्लिप फ़ंक्शन का उपयोग अपने उच्च और निम्न बाइट एरे पर अलग-अलग करने से पहले अलग-अलग करें।

आपकी मदद के लिए धन्यवाद!

+0

अपने मुद्दे को हल करने के लिए ऊपर प्लस -1। –

0

मैं भी एक BufferedImage पहले करने के लिए बाइट सरणी परिवर्तित देखा है, तो यह flipping, लेकिन ऊंचाई और चौड़ाई छवि के वर्तमान संदर्भ में ज्ञात नहीं है।

तुम बस 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 
संबंधित मुद्दे