2010-07-08 14 views
5

अरे दोस्तों, मैं जावा-सक्षम सेल फोन के लिए जे 2 एमई गेम पर काम कर रहा हूं। मैं निम्न विधि के साथ एक पारदर्शी PNG पैमाने पर करने का प्रयास कर रहा हूँ:एक पीएनजी को पुन: सहेजने के लिए कस्टम विधि पारदर्शिता खो देता है

// method derived from a Snippet from http://snippets.dzone.com/posts/show/3257 
// scales an image according to the ratios given as parameters 

private Image rescaleImage(Image image, double XRatio, double YRatio) 
{ 
    // the old height and width 
    int sourceWidth = image.getWidth(); 
    int sourceHeight = image.getHeight(); 

    // what the new height and width should be 
    int newWidth = (int)(XRatio * sourceWidth); 
    int newHeight = (int)(YRatio * sourceHeight); 

    Image newImage = Image.createImage(newWidth, newHeight); 
    Graphics g = newImage.getGraphics(); 

    for (int y = 0; y < newHeight; y++) 
    { 
     for (int x = 0; x < newWidth; x++) 
     { 
      g.setClip(x, y, 1, 1); 
      int dx = (x * sourceWidth)/newWidth; 
      int dy = (y * sourceHeight)/newHeight; 
      g.drawImage(image, (x - dx), (y - dy), Graphics.LEFT | Graphics.TOP); 
     } 
    } 
    return Image.createImage(newImage); 
} 

यह सही ढंग से छवि मापता है, दुर्भाग्य से मैं छवि विधि द्वारा दिया साथ पारदर्शिता खोने लगते हैं। मैं इन अवधारणाओं के लिए बिल्कुल नया हूं, और किसी भी मदद की सराहना की जाएगी! कृपया ध्यान दें कि पर किसी भी जावा-सक्षम मोबाइल डिवाइस पर ठीक से प्रदर्शित होने के लिए, किसी भी प्रकार के छवि संपादक में नहीं, कोड में rescaling को करने की आवश्यकता है।

अग्रिम धन्यवाद!

उत्तर

4

उन सभी लोगों के लिए धन्यवाद जो इस व्यापक रूप से व्यापक और अनसुलझा समस्या के समाधान की तलाश में हैं। मैं http://willperone.net/Code/codescaling.php

पर एक अच्छा समाधान खोजने में कामयाब रहा, आप केवल realRGBImage पैरामीटर में "गलत" को सही पर बदलते हैं। यह अल्फा मानों के रूप में प्रत्येक पिक्सेल के उच्च-आदेश बिट्स को संसाधित करने की विधि को ध्वजांकित करता है। यहां मेरा कार्यान्वयन है, ऊपर दिए गए मूल लिंक से ज्यादा परिवर्तन नहीं।

कैनवास प्रारंभ होने पर XRatio और YRatio को स्थिरांक घोषित किया जाता है, जहां XRatio = this.getWidth() (वर्तमान फोन की स्क्रीन चौड़ाई) आपकी मूल पृष्ठभूमि छवि चौड़ाई से विभाजित है, और GetHeight()/मूल पृष्ठभूमि छवि के साथ YRatio ऊंचाई।

// RESCALEIMAGE 
// scales an image according to the ratios given as parameters 
// derived from http://willperone.net/Code/codescaling.php 

public static Image rescaleImage(Image original, double XRatio, double YRatio) 
{ 
    // the original width and height 
    int originalWidth = original.getWidth(); 
    int originalHeight = original.getHeight(); 

    // the target width and height 
    int newWidth = (int)(XRatio * originalWidth); 
    int newHeight = (int)(YRatio * originalHeight); 

    // create and fill the pixel array from the original image 
    int[] rawInput = new int[originalHeight * originalWidth]; 
    original.getRGB(rawInput, 0, originalWidth, 0, 0, originalWidth, originalHeight); 

    // pixel array for the target image 
    int[] rawOutput = new int[newWidth*newHeight]; 

    // YD compensates for the x loop by subtracting the width back out 
    int YD = (originalHeight/newHeight) * originalWidth - originalWidth; 
    int YR = originalHeight % newHeight; 
    int XD = originalWidth/newWidth; 
    int XR = originalWidth % newWidth; 
    int outOffset= 0; 
    int inOffset= 0; 

    for (int y = newHeight, YE = 0; y > 0; y--) 
    { 
     for (int x = newWidth, XE = 0; x > 0; x--) 
     { 
      rawOutput[outOffset++] = rawInput[inOffset]; 

      inOffset += XD; 
      XE += XR; 

      if (XE >= newWidth) 
      { 
       XE -= newWidth; 
       inOffset++; 
      } 
     } 

     inOffset += YD; 
     YE += YR; 

     if (YE >= newHeight) 
     { 
      YE -= newHeight; 
      inOffset += originalWidth; 
     } 
    } 
    return Image.createRGBImage(rawOutput, newWidth, newHeight, true); 
} 
0

हो सकता है कि छवि वर्ग 'विधि:

Image getScaledInstance(int width, int height, int hints) 

आपको क्या चाहिए के लिए बिल्कुल उपयुक्त है ..?

से: http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/awt/Image.html

+0

मुझे नहीं लगता कि यह विधि जे 2 एमई में समर्थित है, हालांकि, उत्तर के लिए धन्यवाद! – jbabey

+0

मेरी गलती, एक और जवाब पोस्ट करेंगे। –

1

यह है क्योंकि आप पिक्सेल मूल्यों में अल्फा अंतर नहीं करते हैं संभवतः है। आप क्या कर सकते हैं अल्फा के साथ लोगों को संभालने के लिए एक अतिरिक्त दिनचर्या जोड़ें ताकि वे अपने संभावित अल्फा मूल्यों को रख सकें।

असल में यह प्रत्येक पिक्सेल की जांच कर रहा है, देखें कि इसमें अल्फा है, अगर यह है, तो जांच करें कि यह आकार बदल गई छवि पर होगा या नहीं, यदि ऐसा है, तो इसे अल्फा के साथ लागू करें, अगर नहीं, तो इसे छोड़ दें।

+0

धन्यवाद योनाथन। मैं Image.GetRGB() और Image.createRGBImage() को स्केलिंग एल्गोरिदम के साथ एआरबीबी सरणी में हेरफेर करने के लिए उपयोग करने का प्रयास कर रहा हूं। मुझे शुभकामनाएँ :) – jbabey

+0

एआरजीबी दृष्टिकोण पर अपडेट करें: यह तथ्य अच्छी तरह से काम करता है कि स्केलिंग अनुपात आमतौर पर पूर्णांक नहीं होते हैं, जो शुद्ध पिक्सेल सरणी हेरफेर को थोड़ा और कठिन बनाता है। – jbabey