android java tga转png_android tga文件读取

import android.graphics.Bitmap;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

/**

* Created by zhangmike on 1/1/17.

*/

public class TargaReader {

public static Bitmap getImage(String fileName) {

try {

File f = new File(fileName);

byte[] buf = new byte[(int) f.length()];

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));

bis.read(buf);

bis.close();

return decode(buf);

}catch (Exception e){

e.printStackTrace();

return null;

}

}

private static int offset;

private static int btoi(byte b) {

int a = b;

return (a < 0 ? 256 + a : a);

}

private static int read(byte[] buf) {

return btoi(buf[offset++]);

}

public static Bitmap decode(byte[] buf) throws IOException {

offset = 0;

// Reading header bytes

// buf[2]=image type code 0x02=uncompressed BGR or BGRA

// buf[12]+[13]=width

// buf[14]+[15]=height

// buf[16]=image pixel size 0x20=32bit, 0x18=24bit

// buf{17]=Image Descriptor Byte=0x28 (00101000)=32bit/origin

// upperleft/non-interleaved

for (int i = 0; i < 12; i++)

read(buf);

int width = read(buf) + (read(buf) << 8); // 00,04=1024

int height = read(buf) + (read(buf) << 8); // 40,02=576

read(buf);

read(buf);

int n = width * height;

int[] pixels = new int[n];

int idx = 0;

if (buf[2] == 0x02 && buf[16] == 0x20) { // uncompressed BGRA

while (n > 0) {

int b = read(buf);

int g = read(buf);

int r = read(buf);

int a = read(buf);

int v = (a << 24) | (r << 16) | (g << 8) | b;

pixels[idx++] = v;

n -= 1;

}

} else if (buf[2] == 0x02 && buf[16] == 0x18) { // uncompressed BGR

while (n > 0) {

int b = read(buf);

int g = read(buf);

int r = read(buf);

int a = 255; // opaque pixel

int v = (a << 24) | (r << 16) | (g << 8) | b;

pixels[idx++] = v;

n -= 1;

}

} else {

// RLE compressed

while (n > 0) {

int nb = read(buf); // num of pixels

if ((nb & 0x80) == 0) { // 0x80=dec 128, bits 10000000

for (int i = 0; i <= nb; i++) {

int b = read(buf);

int g = read(buf);

int r = read(buf);

pixels[idx++] = 0xff000000 | (r << 16) | (g << 8) | b;

}

} else {

nb &= 0x7f;

int b = read(buf);

int g = read(buf);

int r = read(buf);

int v = 0xff000000 | (r << 16) | (g << 8) | b;

for (int i = 0; i <= nb; i++)

pixels[idx++] = v;

}

n -= nb + 1;

}

}

Bitmap bimg = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

bimg.setPixels(pixels, 0, width, 0, 0, width, height);

return bimg;

}

}

图片操作

Matrix m = new Matirx();

m.postScale(1, -1); //镜像垂直翻转

m.postScale(-1, 1); //镜像水平翻转

m.postRotate(-90); //旋转-90度

Bitmap new2 = Bitmap.createBitmap(a, 0, 0, w, h, m, true);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值