它可以使用Java.awt.Image android应用程序

时间:2015-04-14 15:12:05

标签: java android image graphics awt

我已经编写了一个Java方法,但我必须在android项目中使用这个方法,所以有人可以帮我转换成android或者帮我怎么办?

public Image getImage(){

    ColorModel cm = grayColorModel() ;

    if( n == 1){// in case it's a  8 bit/pixel image 
        return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h,cm, pixData, 0, w));

    }//endif

}

   protected  ColorModel grayColorModel()
   {
    byte[] r = new byte[256] ;
    for (int i = 0; i <256 ; i++ )
        r[i] = (byte)(i & 0xff ) ;
return (new IndexColorModel(8,256,r,r,r));
}

1 个答案:

答案 0 :(得分:0)

例如,要将灰度图像(字节数组,imageSrc)转换为drawable:

        byte[] imageSrc= [...];
        // That's where the RGBA array goes.
        byte[] imageRGBA = new byte[imageSrc.length * 4];
        int i;
        for (i = 0; i < imageSrc.length; i++) {
            imageRGBA[i * 4] = imageRGBA[i * 4 + 1] = imageRGBA[i * 4 + 2] = ((byte) ~imageSrc[i]);

            // Invert the source bits
            imageRGBA[i * 4 + 3] = -1;// 0xff, that's the alpha.
        }

        // Now put these nice RGBA pixels into a Bitmap object
        Bitmap bm = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        bm.copyPixelsFromBuffer(ByteBuffer.wrap(imageRGBA));

代码可能因输入格式而异。