我想将手机摄像头拍摄的图像转换为二进制图像并使用该二进制图像进行进一步处理。我做了一个应用程序,将拍摄照片并将其保存在图库中。
如何将照片转换为二进制文件?
答案 0 :(得分:0)
简单的四行:
Bitmap bmp = BitmapFactory.decodeFile("/imagepath/yourimage.jpg");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] b = bos.toByteArray();
答案 1 :(得分:0)
使用以下方法从相机拍摄图像:
private void fromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
Log.d("FROM CAMERA CLICKED file uri", fileUri.getPath());
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, REQUEST_CODE_FROM_CAMERA);
}
在onActivityResult()之后
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == REQUEST_CODE_FROM_CAMERA
&& resultCode == Activity.RESULT_OK) {
try {
image_path = fileUri.getPath();
Bitmap bm = BitmapFactory.decodeFile(image_path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}