【问题标题】:it's possible to use Java.awt.Image android application可以使用 Java.awt.Image android 应用程序
【发布时间】:2015-06-20 06:44:41
【问题描述】:

我写了一个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));
}

【问题讨论】:

  • 请分享更多信息,您的输入值是多少? pixData 的规格是什么?你想输出什么?
  • pixData[] : 一个向量包含来自 dicom 文件的图像像素,我想将此像素向量转换为图像
  • pixData 是灰度图?
  • pixData 是字节向量 (byte[] pixData;)

标签: java android image graphics awt


【解决方案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));

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

【讨论】:

  • 非常感谢我的朋友,您的代码运行正常,真的很感谢您的帮助
  • 当我在输入值 (imageSrc) 中使用大向量时,它会出现异常(android 在 66073612 字节分配上内存不足)我该怎么办?
  • 我刚刚看过文章。我认为我应该在我的代码中添加“选项”,但我该如何使用它:bitmapImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.imageName, opts);
  • 我认为最好的方法是减少原始图像。因为在 Android 设备上显示 66MB 大小的图像是不必要的。
猜你喜欢
  • 2013-01-04
  • 2023-01-19
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
相关资源
最近更新 更多