|
|
|
|
package com.xypower.mppreview;
|
|
|
|
|
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class RawToJpgConverter {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static List<String> listFiles(File directory) {
|
|
|
|
|
ArrayList list = new ArrayList();
|
|
|
|
|
File[] files = directory.listFiles();
|
|
|
|
|
if (files != null) {
|
|
|
|
|
for (File file : files) {
|
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
|
listFiles(file); // 递归遍历子目录
|
|
|
|
|
} else {
|
|
|
|
|
// 这里可以处理文件,例如打印文件名
|
|
|
|
|
list.add(file.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void convertDngToPng(String dngFile, String pngFile) {
|
|
|
|
|
ImageDecoder.Source src = ImageDecoder.createSource(new File(dngFile));
|
|
|
|
|
Bitmap bmp = null;
|
|
|
|
|
FileOutputStream output = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
bmp = ImageDecoder.decodeBitmap(src);
|
|
|
|
|
output = new FileOutputStream(new File(pngFile));
|
|
|
|
|
bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
closeOutput(output);
|
|
|
|
|
if (bmp != null) {
|
|
|
|
|
bmp.recycle();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Bitmap convertRawToJpg(String path, String outpath) throws IOException {
|
|
|
|
|
// 1. 读取raw资源到字节数组
|
|
|
|
|
InputStream inputStream = new FileInputStream(path);
|
|
|
|
|
byte[] rawData = inputStreamToByteArray(inputStream);
|
|
|
|
|
|
|
|
|
|
// 2. 将字节数组解码为Bitmap
|
|
|
|
|
Bitmap rawBitmap = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
|
|
|
|
|
|
|
|
|
|
// 3. 如果需要,可以在这里对Bitmap进行处理
|
|
|
|
|
|
|
|
|
|
// 4. 创建一个新的字节数组输出流,用于存储JPG格式的图像数据
|
|
|
|
|
FileOutputStream outputStream = new FileOutputStream(outpath);
|
|
|
|
|
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
|
|
// 5. 将Bitmap以JPG格式编码到输出流中
|
|
|
|
|
rawBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytestream);
|
|
|
|
|
|
|
|
|
|
// 6. 将输出流转换为字节数组,并关闭输出流
|
|
|
|
|
byte[] jpgData = bytestream.toByteArray();
|
|
|
|
|
outputStream.write(jpgData);
|
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
|
|
// 7. 返回JPG格式的Bitmap
|
|
|
|
|
return BitmapFactory.decodeByteArray(jpgData, 0, jpgData.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static byte[] inputStreamToByteArray(InputStream inputStream) throws IOException {
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
|
int len;
|
|
|
|
|
while ((len = inputStream.read(buffer)) != -1) {
|
|
|
|
|
byteArrayOutputStream.write(buffer, 0, len);
|
|
|
|
|
}
|
|
|
|
|
byte[] data = byteArrayOutputStream.toByteArray();
|
|
|
|
|
byteArrayOutputStream.close();
|
|
|
|
|
inputStream.close();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
}
|