提高android应用程序中以编程方式捕获的图像的质量[重复]
Posted
技术标签:
【中文标题】提高android应用程序中以编程方式捕获的图像的质量[重复]【英文标题】:Improve quality of programmatically captured image in the android app [duplicate] 【发布时间】:2019-01-14 12:28:15 【问题描述】:我在我的应用程序中集成了相机,并使用该相机拍摄的图像是模糊的。有关提高捕获图像质量的任何建议。 我正在使用 multipart 在服务器上发送图像。
代码片段
@Override
public void openCameraAction()
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
photo = (Bitmap) data.getExtras().get("data");
imageBase64 = encodeToBase64(photo);
imageUri = getImageUri(getApplicationContext(), photo);
imageFile = new File(getRealPathFromURI(imageUri));
getImageView.setImageBitmap(photo);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),imageFile);
MultipartBody.Part image = MultipartBody.Part.createFormData(imageQuestionId, imageFile.getName(),requestBody);
parts.add(image);
public static String encodeToBase64(Bitmap image)
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
public Uri getImageUri(Context inContext, Bitmap inImage)
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
public String getRealPathFromURI(Uri uri)
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
【问题讨论】:
谁能帮我解决这个问题? 您要上传从相机拍摄的实际图像吗? @GarrimaKakkr 检查此***.com/a/11049378/7666442 @GarrimaKakkr 还有这个***.com/a/10382217/7666442 @NileshRathod 谢谢 【参考方案1】:在这个要点中找到一个类图像选择器 https://gist.github.com/r00786/2c9aa88b706daccd55098ac2c28e7f39
所有的事情都在这个类中处理
如何使用
private static final int PICK_IMAGE_ID = 234; // the number doesn't matter
public void onPickImage(View view)
Intent chooseImageIntent = ImagePicker.getPickImageIntent(this);
startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
switch(requestCode)
case PICK_IMAGE_ID:
Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data);
// TODO use bitmap
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
【讨论】:
【参考方案2】:如果您希望将相机拍摄的实际图像发送到服务器,您需要创建 图片
试试这个代码
删除这个变量
private String actualPictureImagePath = "";
然后在按钮点击时调用这个方法cameraIntent()
private void cameraIntent()
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
actualPictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(pictureImagePath);
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 1);
然后在onActivityResult()
处理这个
@override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == 1)
File imgFile = new File(actualPictureImagePath);
if(imgFile.exists())
InputStream inputStream = null;//You can get an inputStream using any IO API
inputStream = new FileInputStream(imgFile.getAbsolutePath());
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);
try
while ((bytesRead = inputStream.read(buffer)) != -1)
output64.write(buffer, 0, bytesRead);
catch (IOException e)
e.printStackTrace();
output64.close();
String base64String = output.toString();
这是用于 Base64 位图的代码
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm
is the bitmap object
byte[] byteArrayImage = baos.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage,
Base64.DEFAULT);
注意:-
不要忘记在清单中添加运行时权限
1)读写权限
2)相机权限
【讨论】:
以上是关于提高android应用程序中以编程方式捕获的图像的质量[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 中以编程方式将图像文件从图库复制到另一个文件夹
在 Android 中以编程方式实现 android:src="@drawable/image"