| | |
| | | |
| | | import java.io.BufferedInputStream; |
| | | import java.io.BufferedOutputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.nio.ByteBuffer; |
| | | import java.util.Locale; |
| | | |
| | | import android.content.Context; |
| | | import android.graphics.Bitmap; |
| | | import android.graphics.BitmapFactory; |
| | | import android.graphics.ImageFormat; |
| | | import android.graphics.Matrix; |
| | | //import android.support.media.ExifInterface; |
| | | import android.graphics.Rect; |
| | | import android.graphics.YuvImage; |
| | | import android.media.ExifInterface; |
| | | import android.media.Image; |
| | | import android.os.Environment; |
| | | import android.renderscript.RenderScript; |
| | | import android.renderscript.ScriptIntrinsicYuvToRGB; |
| | | |
| | | import static android.os.Environment.DIRECTORY_DOCUMENTS; |
| | | |
| | | public class BitmapUtil { |
| | | |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private static ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| | | /**将nv21数据转换为Bitmap*/ |
| | | public static Bitmap nv21ToBitmap(byte[] nv21, int width, int height) { |
| | | Bitmap bitmap = null; |
| | | YuvImage image = new YuvImage(nv21, ImageFormat.NV21, width, height, null); |
| | | //输出到对应流 |
| | | image.compressToJpeg(new Rect(0, 0, width, height), 100, stream); |
| | | //对应字节流生成bitmap |
| | | bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size()); |
| | | //避免中间的byte数组转换步骤,直接从YuvImage写入ByteBuffer,然后使用BitmapFactory的相应方法直接从ByteBuffer创建Bitmap |
| | | stream.reset(); |
| | | return bitmap; |
| | | } |
| | | |
| | | /**保存bitmap到文件*/ |
| | | public static void saveBitmapToFile(Bitmap bitmap) { |
| | | // 定义图片的保存路径和文件名 |
| | | String fileName = "IMG_" + System.currentTimeMillis() + ".jpg"; |
| | | String filePath = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS).getAbsolutePath() + "/yc_test"+ File.separator + fileName; |
| | | try( |
| | | FileOutputStream fos = new FileOutputStream(filePath); |
| | | BufferedOutputStream bos = new BufferedOutputStream(fos); |
| | | ){ |
| | | bitmap.compress(Bitmap.CompressFormat.JPEG, 99, bos); |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | }finally { |
| | | bitmap.recycle(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 避免中间的byte数组转换步骤,直接从YuvImage写入ByteBuffer,然后使用BitmapFactory的相应方法直接从ByteBuffer创建Bitmap |
| | | * */ |
| | | /*public static Bitmap yuvToBitmap(Image image, int width, int height) { |
| | | //获取YUV数据 |
| | | *//*int I420size = image.getWidth()*image.getHeight()*3/2; |
| | | byte[] nv21 = new byte[I420size]; |
| | | Image.Plane[] planes = image.getPlanes(); |
| | | int remaining0 = planes[0].getBuffer().remaining(); |
| | | int remaining1 = planes[1].getBuffer().remaining(); |
| | | int remaining2 = planes[2].getBuffer().remaining(); |
| | | //分别准备三个数组接收YUV分量。 |
| | | byte[] yRawSrcBytes = new byte[remaining0]; |
| | | byte[] uRawSrcBytes = new byte[remaining1]; |
| | | byte[] vRawSrcBytes = new byte[remaining2]; |
| | | planes[0].getBuffer().get(yRawSrcBytes); |
| | | planes[1].getBuffer().get(uRawSrcBytes); |
| | | planes[2].getBuffer().get(vRawSrcBytes);*//* |
| | | |
| | | int W = image.getWidth(); |
| | | int H = image.getHeight(); |
| | | |
| | | Image.Plane Y = image.getPlanes()[0]; |
| | | Image.Plane U = image.getPlanes()[1]; |
| | | Image.Plane V = image.getPlanes()[2]; |
| | | |
| | | int Yb = Y.getBuffer().remaining(); |
| | | int Ub = U.getBuffer().remaining(); |
| | | int Vb = V.getBuffer().remaining(); |
| | | |
| | | byte[] data = new byte[Yb + Ub + Vb]; |
| | | |
| | | |
| | | Y.getBuffer().get(data, 0, Yb); |
| | | V.getBuffer().get(data, Yb, Vb); |
| | | U.getBuffer().get(data, Yb + Vb, Ub); |
| | | |
| | | |
| | | |
| | | RenderScript rs = RenderScript.create(context); |
| | | ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs)); |
| | | |
| | | Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(data.length); |
| | | Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT); |
| | | |
| | | Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(W).setY(H); |
| | | Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT); |
| | | |
| | | final Bitmap bmpout = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888); |
| | | |
| | | in.copyFromUnchecked(data); |
| | | |
| | | yuvToRgbIntrinsic.setInput(in); |
| | | yuvToRgbIntrinsic.forEach(out); |
| | | out.copyTo(bmpout); |
| | | image.close(); |
| | | return bmpout ; |
| | | }*/ |
| | | } |