package hello;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("start");
System.out.println("最小值:Byte.MIN_VALUE=" + Byte.MIN_VALUE);
System.out.println("最大值:Byte.MAX_VALUE=" + Byte.MAX_VALUE);
byte t=(byte) 0xff;
int tt=t&((int)0xff);
System.out.println("test tt=" + tt);
File directory = new File("");//设定为当前文件夹
try{
System.out.println(directory.getCanonicalPath());//获取标准的路径
System.out.println(directory.getAbsolutePath());//获取绝对路径
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("========================");
String filename=directory.getAbsolutePath();
filename +="\\face.bmp";
int bmp_len=getFileLength(filename);
byte[] getBytes = new byte[bmp_len];
myPic param= new myPic(640,480);
getBytes=readBmp(filename,param);
int w= param.getW();//640;
int h= param.getH();//480;
int yuv_len=w*h*3/2;
System.out.printf("%dx%d \n",w,h);
//writePicToFile("old.bmp1",getBytes,w*h*3,1);
byte[] nv21_data = new byte[yuv_len];
encodeNV21(nv21_data,getBytes,w,h,true);
writePicToFile("face.nv21",nv21_data,yuv_len,0);
encodeNV21(nv21_data,getBytes,w,h,false);
writePicToFile("face.nv12",nv21_data,yuv_len,0);
System.out.println("end");
}
public static void writePicToFile(String name,byte[] buf, int len,int type) {
try {
//String fdir=getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath();
//String fdir=Environment.getExternalStorageDirectory().toString()+"/DCIM/Camera";
//String fdir="C:\\Users\\fugong.sun\\eclipse-workspace\\hello\\src\\hello";
File directory = new File("");
String path = directory.getAbsolutePath()+"\\"+name;
File file = new File(path);
if (!file.exists()) {
File dir = new File(file.getParent());
dir.mkdirs();
file.createNewFile();
}
FileOutputStream outStream = new FileOutputStream(file, false);
outStream.write(buf,0,len);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte [] readBmp(String filename, myPic pr)
{
byte[] getBytes = {};
byte[] gethead = {};
try {
File dirFile = new File(filename);
if (!dirFile.exists()) {
System.out.println( filename ) ;
}
int bmpHeadSize=54;
gethead = new byte [bmpHeadSize];
int dataLen=(int)dirFile.length();
dataLen -=bmpHeadSize;
getBytes = new byte [dataLen];
FileInputStream inStream = new FileInputStream(dirFile);
int size = inStream.available();
int readBytes = inStream.read(gethead,0,bmpHeadSize);
int w = byte2int(gethead[19])<<8;
w += byte2int(gethead[18]);
int h = byte2int(gethead[23])<<8;
h += byte2int(gethead[22]);
pr.setW(w);
pr.setH(h);
System.out.printf("read w*h=%dx%d \n",w,h);
readBytes=inStream.read(getBytes,0,dataLen);
/*
BufferedInputStream bis=new BufferedInputStream(inStream);
bis.skip(bmpHeadSize);
int readBytes = bis.read(getBytes,0,dataLen);
*/
inStream.close();
}catch (Exception e) {
e.printStackTrace();
}
return getBytes;
}
public static int getFileLength(String filename)
{
int dataLen=0;
try {
File dirFile = new File(filename);
if (!dirFile.exists()) {
System.out.println( filename ) ;
}
dataLen=(int)dirFile.length();
}catch (Exception e) {
e.printStackTrace();
}
return dataLen;
}
private static void encodeNV21(byte[] yuv420sp, byte[] argb, int width, int height,boolean bNv21) {
final int frameSize = width * height;
int Y, U, V;
int yIndex = 0;
int uvIndex = frameSize;
int R, G, B;
int argbIndex = 0;
byte[] rgb24= new byte[width*height*3];
int t=0;
for (int j = height-1,k=0; j >=0; j--,k++) {
for (int i = 0; i < width;i++) {
argbIndex =(j*width+i)*3;
t=(k*width+i)*3;
rgb24[t]= argb[argbIndex];
rgb24[t+1]= argb[argbIndex+1];
rgb24[t+2]= argb[argbIndex+2];
}
//k++;
}
//writePicToFile("rt.bmp2",rgb24,width*height*3,1);
argbIndex=0;
int ypos=0;
for (int j = 0; j <height; j++)
{
for (int i = 0; i < width;i++)
{
ypos=j*width+i;
argbIndex =ypos*3;
R = byte2int(rgb24[argbIndex]);
G = byte2int(rgb24[argbIndex+1]);
B = byte2int(rgb24[argbIndex+2]);
// well known RGB to YUV algorithm,
/*Y = (byte)((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = (byte)((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = (byte)((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;*/
// Improvement above algorithm
Y = (306 * R + 601 * G + 117 * B) >> 10;
U = ((-173 * R - 339 * G + 512 * B) >> 10) + 128;
V = ((512 * R - 429 * G - 83 * B) >> 10) + 128;
Y = MAX(Y, 16);
Y = VALID(Y);
U = VALID(U);
V = VALID(V);
// NV21 has a plane of Y and interleaved planes of VU each
// sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the
// sampling is every other
// pixel AND every other scanline.
// ---Y---
yuv420sp[yIndex++] = (byte)(Y);
// yuv is ok
//yuv420sp[yIndex++] = (byte) U;
//yuv420sp[yIndex++] = (byte) V;
if (((j & 1) == 1)&&((i & 1 ) == 1))
{
int index = argbIndex-3;
int []u={0,0,0};
int []v={0,0,0};
int r,g,b;
int uc=0;
int vc=0;
for(int a=0;a<3;a++)
{
if(a==0)
index = argbIndex-3;
else if(a==1)
index = argbIndex-width*3;
else
index = argbIndex-width*3-3;
r = byte2int(rgb24[index]);
g = byte2int(rgb24[index+1]);
b = byte2int(rgb24[index+2]);
u[a] = ((-173 * r - 339 * g + 512 * b) >> 10) + 128;
v[a] = ((512 * r - 429 * g - 83 * b) >> 10) + 128;
u[a] = VALID(u[a]);
v[a] = VALID(v[a]);
uc +=u[a];
vc +=v[a];
}
if(bNv21)
{
yuv420sp[uvIndex++] = (byte) ((U+uc)/4);
yuv420sp[uvIndex++] = (byte) ((V+vc)/4);
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
bmp/rgb24转成nv21/nv12的java代码,经实际测试是正确的,网上很多提供的JAVA代码实际上转换成来的格式不对,附件里是java的完整代码,直接编译即可生成face.nv21 face.nv12两种格式文件,可使用yuvplayer.exe打开相应文件验证是否正确,也可替换工程里的face.bmp.
资源推荐
资源详情
资源评论
收起资源包目录
java_BMPtoNV21.rar (14个子文件)
hello
face.nv12 450KB
.project 381B
face.nv21 450KB
face.bmp 900KB
src
module-info.java 69B
hello
face.nv12 450KB
face.bmp 900KB
myPic.java 325B
HelloWorld.java 9KB
.settings
org.eclipse.jdt.core.prefs 642B
.classpath 396B
bin
hello
HelloWorld.class 6KB
myPic.class 738B
module-info.class 144B
共 14 条
- 1
资源评论
偷懒的人
- 粉丝: 17
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功