android java tga转png_java 里.tga图片怎么用啊,是不是要导入一个包,真的找不到资源,希望来位大神帮帮我 小弟在此拜谢!...

展开全部

struct sTGAHEADER

{

//TGA文件头结构

public byte id_length;

public byte colormap_type;

public byte image_type;

public ushort colormap_index;

public ushort colormap_length;

public byte colormap_size;

public ushort x;

public ushort y;

public ushort width;

public ushort heigth;

public byte pixel_size;

public byte attributes;

}

//读取TGA文件的函数,62616964757a686964616fe78988e69d8331333361306338将图象解码后写入内存Bitmap类型中

public static bool OpenTGAFile(byte[] tagContent, ref Bitmap bmp)

{

sTGAHEADER th;

MemoryStream fs = new MemoryStream(tagContent);

BinaryReader br = new BinaryReader(fs);

//读取文件头,因为我不知道如何从流中读取自定义数据结构,只好一个一个读了

th.id_length = br.ReadByte();

th.colormap_type = br.ReadByte();

th.image_type = br.ReadByte();

th.colormap_index = br.ReadUInt16();

th.colormap_length = br.ReadUInt16();

th.colormap_size = br.ReadByte();

th.x = br.ReadUInt16();

th.y = br.ReadUInt16();

th.width = br.ReadUInt16();

th.heigth = br.ReadUInt16();

th.pixel_size = br.ReadByte();

th.attributes = br.ReadByte();

if (th.pixel_size != 24 & th.pixel_size != 32)

{

MessageBox.Show("只支持24位和32位的图象格式。");

return false;

}

int x;

int y;

int dest;

byte[] p = new byte[4];

//读取颜色值的临时变量

byte[] p24;

fs.Seek(th.id_length, SeekOrigin.Current);

//定位文件指针跳过文件信息

int ByteCount = 4 * th.width * th.heigth;

//目标始终是32位格式

byte[] Bytes = new byte[ByteCount];

//分配临时内存

if (th.image_type == 2)

{

//不压缩格式,直接读入每个象素的颜色值

dest = 0;

for (y = th.heigth - 1; y >= 0; y += -1)

{

//图象是上下倒置的

dest = y * th.width * 4;

for (x = 0; x <= th.width - 1; x++)

{

if (th.pixel_size == 24)

{

p24 = br.ReadBytes(3);

//24位读入3个字节, 它会改变数组P的维数

p[0] = p24[0];

p[1] = p24[1];

p[2] = p24[2];

p[3] = 255;

}

else

{

p = br.ReadBytes(4);

//32位格式,读入4个字节

}

Bytes[dest] = p[0];

Bytes[dest + 1] = p[1];

Bytes[dest + 2] = p[2];

Bytes[dest + 3] = p[3];

dest += 4;

}

}

}

else if (th.image_type == 10)

{

//RLE压缩

//TGA文件RLE压缩的方式为,从最下一行向上记录,块标识如果大于127,后面为一个颜色值,

//表示之后的标识减去127个象素都是这个颜色;如果标识小于128,则后面标识数量的象素为没有压缩的颜色值。

byte PacketHeader;

int PacketSize;

int a;

int i;

int j;

j = (int)th.width * (int)th.heigth;

i = 0;

do

{

PacketHeader = br.ReadByte();

//读入一个字节

if (PacketHeader >= 128)

PacketSize = (PacketHeader - 128);

else

PacketSize = PacketHeader;

for (a = 0; a <= PacketSize; a++)

{

//循环块

if (PacketHeader < 128 | a == 0)

{

//不是压缩块,每次都要读入数值

if (th.pixel_size == 24)

{

p24 = br.ReadBytes(3);

//24位读入3个字节, 它会改变数组P的维数

p[0] = p24[0];

p[1] = p24[1];

p[2] = p24[2];

p[3] = 255;

}

else

{

p = br.ReadBytes(4);

//32位格式,读入4个字节

}

}

y = th.heigth - (i / th.width) - 1;

x = i % th.width;

dest = (y * th.width + x) * 4;

Bytes[dest] = p[0];

Bytes[dest + 1] = p[1];

Bytes[dest + 2] = p[2];

Bytes[dest + 3] = p[3];

i += 1;

if (i == j)

break; // TODO: might not be correct. Was : Exit Do

}

}

while (true);

}

else

{

MessageBox.Show("文件格式不支持。");

return false;

}

//所有数据已经读入bytes,创建离屏表面

if ((bmp != null))

{

//释放之前的内存

bmp.Dispose();

bmp = null;

}

bmp = new Bitmap(th.width, th.heigth, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

System.Drawing.Imaging.BitmapData bmpData;

//锁定内存

bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);

//得到内存指针

IntPtr ptr = bmpData.Scan0;

//拷贝数据到指针内存

System.Runtime.InteropServices.Marshal.Copy(Bytes, 0, ptr, ByteCount);

//解锁

bmp.UnlockBits(bmpData);

//翻转

bmp.RotateFlip(RotateFlipType.Rotate180FlipX);

return true;

}

追问

大神,这个我不会用,能不能给我说说?

追答

运行下debug!

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值