java - 如何在 Android 中拍照,使用完整图像大小(不是缩略图)并将图像压缩为字节?
问题描述
我不知道我是否做错了什么,也许我根本没有使用图像,或者我以错误的方式压缩图像,因为当我试图将它发送到服务器时,它回复我说当我的手机占用时大小超过 10 MB图片 jpg 大约 7-9 MB(在 Edit.java 中我有一条评论,我以前使用缩略图但需要更改它,因为当我尝试在桌面上查看缩略图时,缩略图质量很差)
这是我的代码:
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"></uses-feature>
<provider
android:authorities="cam.com.example.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path"/>
</provider>
文件路径.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="/"/>
<external-files-path
name="external_files"
path="/"/>
<cache-path
name="cache"
path="/"/>
<external-cache-path
name="external_cache"
path="/"/>
<files-path
name="files"
path="/"/>
</paths>
编辑.java
btn_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
REQUEST_IMAGE_CAPTURE = 1;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
File imageFile = null;
try{
imageFile=getImageFile();
}catch (IOException e){
e.printStackTrace();
}
if(imageFile!=null){
Uri imageUri = FileProvider.getUriForFile(Edit.this,"cam.com.example.fileprovider",imageFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
} catch (Exception e) {
Toasty.warning(getApplicationContext(), IC, Toast.LENGTH_SHORT, true).show();
}
}
});
public File getImageFile() throws IOException{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageName = "jpg_"+timeStamp+"_";
File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File imageFile = File.createTempFile(imageName,".jpg",storageDir);
currentImagePath = imageFile.getAbsolutePath();
return imageFile;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
if (imagenString == null) {
File imgFile = new File(currentImagePath);
String path = imgFile.getAbsolutePath();
Bitmap mybitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
mybitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
imagenString = Base64.encodeToString(byteArray, Base64.DEFAULT);
/* **Before I was doing this, but the thumbnail has such a bad quality so needed to change it**
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
imagenString = Base64.encodeToString(byteArray, Base64.DEFAULT);*/
}
}
}
解决方案
您正在使用用户选择的相机应用程序拍照。通常,这将保存为 JPEG 图像。JPEG 图像是压缩图像格式,针对照片等“真实世界”图像进行了优化。
然后,您尝试将其全部读入内存。这不是一个好计划,因为您可能没有足够的内存来制作全尺寸照片。
然后,您尝试获取结果Bitmap
并将其编码为 PNG。PNG 是一种压缩图像格式,但它是为图标和其他艺术品设计的。一张照片在 PNG 中几乎总是比在 JPEG 中占用更多空间。更糟糕的是,您正试图将其作为 PNG 编码到内存中——同样,您可能没有足够的内存来执行此操作。
然后,您尝试将编码的 PNG 转换为 base-64。这将比编码的 PNG 占用更多的空间,而且,你可能没有足够的内存来做这件事。
我希望您的应用程序崩溃OutOfMemoryError
很多。
最好的解决方案是摆脱其中的大部分,直接从磁盘上传 JPEG。不要将其加载到内存中,不要将其转换为 PNG,也不要将其转换为 base-64。
推荐阅读
- python-2.7 - 使用 Selenium Python 复制文本字段值时,HTML 表单不返回剪贴板?
- ios - 为 iPhone 应用程序获取 .IPA 文件
- c++ - 从 .ini 加载数据使我的游戏崩溃!我能做些什么?C++
- spring-mvc - 设置了 DB 毫秒值的 Spring 调度程序
- c# - 将字符串分隔到不在集合字典中的列表中
- java - 具有 2 个过滤条件的 java 流
- python - 从其他函数调用函数
- ruby - 为什么'p'打印最后一个变量的值?
- c# - EF 通用存储库模式 - 插入带有子实体的根实体时出现插入/创建方法问题
- reactjs - 反应 HTML 表格