2012-02-20 14 views
5

के साथ जावा स्विट छवियों के भाग कॉपी करना मैं पूर्ण 32-बिट रंग और अल्फा चैनल (org.eclipse.swt.graphics.Image) के साथ जावा swt छवियों का उपयोग कर रहा हूं। मुझे पूर्ण अल्फा चैनल जानकारी सहित, छवि की आयत को एक नई छवि के रूप में निकालने की आवश्यकता है।अल्फा

उदाहरण के लिए, मेरे पास एक छवि का आकार 100 × 100 हो सकता है, और मुझे केवल 100-100 छवियों में शीर्ष बाएं 50 × 50 पिक्सेल के साथ एक नई छवि बनाने की आवश्यकता है। (मैं चिपक रहा हूं, rescaling नहीं।)

मेरा मूल प्रयास मुझे आवश्यक आकार की एक खाली छवि (इस उदाहरण में 50 × 50) बनाना था, फिर नई छवि के लिए जीसी प्राप्त करें, फिर सही बनाएं नई छवि पर पुरानी छवि का हिस्सा। समस्या यह थी कि पारदर्शिता संरक्षित नहीं थी। अगर मेरी नई छवि अपारदर्शी शुरू हुई, तो ड्रॉ के बाद परिणाम पूरी तरह से अपारदर्शी होगा। अगर मेरी नई छवि पारदर्शी शुरू हुई, तो ड्रॉ के बाद परिणाम पूरी तरह से पारदर्शी होगा (सभी आरजीबी रंगों के साथ सही, लेकिन अदृश्य है क्योंकि अल्फा सभी पिक्सल के लिए शून्य होगा)।

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

/** 
* Returns a sub-rectangle from the image, preserving alpha transparency. 
* @param source The image from which to copy a portion 
* @param sourceData The ImageData of the source image 
* @param xInSource The x-coordinate in the source image from which to start copying 
* @param yInSource The y-coordinate in the source image from which to start copying 
* @param widthInSource The width (in pixels) of the copied image portion 
* @param heightInSource The height (in pixels) of the copied image portion 
* @param disp The display to use for the new image, or null for the current display 
* @return New image copied from a portion of the source. 
*/ 
private static Image getRectHelper(final Image source, final ImageData sourceData, 
     final int xInSource, final int yInSource, final int widthInSource, final int heightInSource, final Display disp) 
{ 
    // In cases like extracting multiple frames at once, we need both the Image and its ImageData without 
    // having to extract the ImageData over and over. That’s why this helper takes both parameters. 

    // New up an image of the appropriate size 
    final Display d = disp==null ? Display.getDefault() : disp; 
    final Image dest = new Image(d, widthInSource, heightInSource); 

    // This draws the colors from the original image, but does not set the destination alphas 
    // (For the Image class, alpha transparency doesn’t work well – it seems to have been tacked on as an afterthought.) 
    final GC g = new GC(dest); 
    g.drawImage(source, xInSource, yInSource, widthInSource, heightInSource, 0, 0, widthInSource, heightInSource); 
    g.dispose(); 

    // Get a copy of the dest image data, then sets the destination alphas from the source image 
    final ImageData destData = dest.getImageData(); 
    copyAlpha(sourceData, destData, xInSource, yInSource, widthInSource, heightInSource, 0, 0); 

    // Construct a new image with the modified image data 
    final Image ret = new Image(d, destData); 
    dest.dispose(); 

    return ret; 
} 

/** 
* Copies a block of alpha channel information from the sourceData to the destaData. 
* 
* @param sourceData The source image data from which to copy alpha information 
* @param destData The destination image data to modify with new alpha information 
* @param xInSource The x-coordinate from which the alpha information should be copied 
* @param yInSource The y-coordinate from which the alpha information should be copied 
* @param width The width (in pixels) of the alpha information to copy 
* @param height The height (in pixels) of the alpha information to copy 
* @param xInDest The x-coordinate to which the alpha information should be copied 
* @param yInDest The y-coordinate to which the alpha information should be copied 
*/ 
private static void copyAlpha(final ImageData sourceData, final ImageData destData, 
     final int xInSource, final int yInSource, final int width, final int height, 
     final int xInDest, final int yInDest) 
{ 
    final byte[] alphas = new byte[width]; 
    for (int ySrc = yInSource, yDst = yInDest; ySrc < yInSource+height; ++ySrc, ++yDst) { 
     sourceData.getAlphas(xInSource, ySrc, width, alphas, 0); 
     destData.setAlphas(xInDest, yDst, width, alphas, 0); 
    } 
} 

मुझे यहां क्या याद आ रही है? मैं जावा में नया हूं, इसलिए मैं कुछ गलत कर रहा हूं। क्या अल्फा चैनल वास्तव में स्विट छवि वर्ग के लिए एक विचारधारा है? निश्चित रूप से ऐसा करने के लिए एक आसान और तेज़ तरीका है। मैं सीधे ImageData ऑब्जेक्ट्स का उपयोग करने के लिए प्रेरित हूं, और पूरी तरह से जीसी उपयोग को बाईपास कर रहा हूं।

उत्तर

0

तो एक चीज जो आपको याद आ रही है वह यह है कि छवि के लिए कई पारदर्शिता प्रकार हैं। http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/ImageData.html#getTransparencyType()

आकार बदलने के कोड में हम एसडब्ल्यूटी छवियों के लिए उपयोग करते हैं, हम पहले एक नई छवि बनाते हैं और इसकी छविडेटा प्राप्त करते हैं।

  • SWT.TRANSPARENCY_ALPHA, तो अपने उदाहरण चाल करना चाहिए की तरह एक अल्फा लागू करने हैं: तो फिर मूल छवि के ImageData की पारदर्शिता प्रकार के आधार पर, हम निम्नलिखित बातों में से एक है।
  • यदि SWT.TRANSPARENCY_PIXEL है, तो हमने मूल छवि की छविडेटा से मेल खाने के लिए ImageData की पारदर्शी पिक्सेल संपत्ति सेट की है।
  • यदि SWT.TRANSPARENCY_MASK है, तो हमने मूल छवि के छविडेटा से मेल खाने के लिए नए ImageData के मास्कडेटा और मास्कपैड गुण सेट किए हैं।

हम फिर एक नई छवि (पहले का निपटान) बनाने के लिए ImageData का उपयोग करते हैं और जीसी का उपयोग करके इसे आकर्षित करते हैं।

उम्मीद है कि उपर्युक्त समस्या को हल करने के करीब पहुंचने में आपकी सहायता कर सकता है।

संबंधित मुद्दे