有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Java是可能的。awt。图像安卓应用程序

我已经编写了一个Java方法,但是我必须在安卓项目中使用这个方法,所以有人可以帮助我将其转换为安卓,或者帮助我该怎么做

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) 个答案

  1. # 1 楼答案

    例如,要将灰度图像(字节数组,imageSrc)转换为可绘制图像,请执行以下操作:

            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));
    

    根据输入格式的不同,代码可能会有所不同