| | |
| | | |
| | | import com.whyc.util.BitmapUtil; |
| | | |
| | | import java.io.BufferedOutputStream; |
| | | import java.io.File; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.nio.ByteBuffer; |
| | |
| | | mainCameraDevice = cameraDevice; |
| | | |
| | | //拍照的接收器 |
| | | mImageReader = ImageReader.newInstance(outputSizes.getWidth(), outputSizes.getHeight(), ImageFormat.YUV_420_888, 2); |
| | | mImageReader = ImageReader.newInstance(outputSizes.getWidth(), outputSizes.getHeight(), ImageFormat.JPEG, 2); |
| | | // mImageReader = ImageReader.newInstance(outputSizes.getWidth(), outputSizes.getHeight(), ImageFormat.JPEG, 4); |
| | | //创建主摄像头会话并预览 |
| | | createPreviewSession(); |
| | |
| | | // llUpText.setVisibility(View.VISIBLE); |
| | | // tvDevice.setText("这是测试的动态赋值"); |
| | | // 保存图片到相册-这种方式是JEPG格式 |
| | | // saveImageToGallery(image); |
| | | saveImageToGallery(image); |
| | | //YUV420 |
| | | |
| | | // int I420size = image.getWidth()*image.getHeight()*3/2; |
| | | // byte[] nv21 = new byte[I420size]; |
| | | // YUVUtil.YUVToNV21_NV12(image,nv21,outputSizes.getWidth(),outputSizes.getHeight(),"NV21"); |
| | | List<byte[]> planetBytes = getPlanetBytes(image); |
| | | /*List<byte[]> planetBytes = getPlanetBytes(image); |
| | | byte[] nv21 = byteMergerAll(planetBytes.get(0), planetBytes.get(1)); |
| | | Bitmap bitmap = BitmapUtil.nv21ToBitmap(nv21, outputSizes.getWidth(), outputSizes.getHeight()); |
| | | BitmapUtil.saveBitmapToFile(bitmap); |
| | | BitmapUtil.saveBitmapToFile(bitmap);*/ |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | }finally { |
| | |
| | | byte[] data = new byte[buffer.remaining()]; |
| | | buffer.get(data); |
| | | // 创建文件输出流 |
| | | try { |
| | | // FileOutputStream fos = null; |
| | | /*try { |
| | | fos = new FileOutputStream(filePath); |
| | | fos.write(data);*/ |
| | | try ( |
| | | FileOutputStream fos = new FileOutputStream(filePath); |
| | | fos.write(data); |
| | | fos.close(); |
| | | image.close(); |
| | | // 通知图库更新 |
| | | BufferedOutputStream bos = new BufferedOutputStream(fos)){ |
| | | bos.write(data); |
| | | } catch (FileNotFoundException fileNotFoundException) { |
| | | fileNotFoundException.printStackTrace(); |
| | | } catch (IOException ioException) { |
| | | ioException.printStackTrace(); |
| | | } |
| | | try { |
| | | // 通知图库更新 |
| | | MediaScannerConnection.scanFile(mContext, new String[]{filePath}, null, null); |
| | | |
| | | // 在某些设备上,可能需要发送广播通知才能使图片立即出现在相册中 |
| | |
| | | |
| | | // 显示保存成功的提示 |
| | | // Toast.makeText(this, "图片保存成功", Toast.LENGTH_SHORT).show(); |
| | | } catch (IOException e) { |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // 显示保存失败的提示 |
| | | // Toast.makeText(this, "图片保存失败", Toast.LENGTH_SHORT).show(); |
| | | }finally { |
| | | image.close(); |
| | | } |
| | | } |
| | | |