Kis*_*oid 0 android bytearray bitmap android-camera android-camera-intent
从最近几天开始,我遇到的相机图像问题太小,尺寸为120 x 160.
场景是当我从相机捕获图像并将该图像转换为byte []时,它变得太小.
以下是我使用的代码:
Bitmap unscaledBitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
unscaledBitmap .compress(Bitmap.CompressFormat.PNG, 30, bytes);
byte[] bytesDoc1 = bytes.toByteArray();
Run Code Online (Sandbox Code Playgroud)
对此没有任何了解,但是我已经尝试了许多解决方案,但都没有为我工作.
请帮我.
您需要指定相机意图存储实际图像的路径.因为data.getExtras().get("data");只是从真实图像中获取缩略图.
看看这段代码:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create the File where the photo should go
File photoFile = createImageFile();
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, 1);
}
Run Code Online (Sandbox Code Playgroud)
这是创建文件的方法:
private File createImageFile() {
long timeStamp = System.currentTimeMillis();
String imageFileName = "NAME_" + timeStamp;
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以阅读创建的图片的真实路径.