Android 安卓相机的图像专家
一直在尝试将高质量的压缩图像上传到S3,但它一直被删除 这是我的黑客代码在一起已经有几天了,所以会喜欢一些新鲜的眼睛。一直在努力保持每张图片的精确保存大小,并保持纵横比不变 但似乎会影响质量和分辨率Android 安卓相机的图像专家,android,image,android-camera,Android,Image,Android Camera,一直在尝试将高质量的压缩图像上传到S3,但它一直被删除 这是我的黑客代码在一起已经有几天了,所以会喜欢一些新鲜的眼睛。一直在努力保持每张图片的精确保存大小,并保持纵横比不变 但似乎会影响质量和分辨率 private void saveImage(){ if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) { pictureFile = getOu
private void saveImage(){
if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) {
pictureFile = getOutputMediaFile();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
int num = Exif.getOrientation(data);
if (num > 0) {
bitmap = rotateImage(bitmap, num);
}
try {
Bitmap scaledBitmap = scaleImage(bitmap);
FileOutputStream fos = new FileOutputStream(pictureFile);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Uri contentUri = Uri.fromFile(pictureFile);
saveImageLocation(contentUri.getPath());
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
getActivity().sendBroadcast(mediaScanIntent);
} catch (Exception e) {
Toast.makeText(getActivity(), "Could not save your image!",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(),
"Your phone doesn't have space for image",
Toast.LENGTH_SHORT).show();
}
}
}
private Bitmap rotateImage(Bitmap image, int rotate) {
Toast.makeText(getActivity(), "rotate", Toast.LENGTH_SHORT).show();
int w = image.getWidth();
int h = image.getHeight();
Matrix mtx = new Matrix();
mtx.preRotate(rotate);
image = Bitmap.createBitmap(image, 0, 0, w, h, mtx, false);
return image.copy(Bitmap.Config.ARGB_8888, true);
}
private Bitmap scaleImage(Bitmap image) {
int height = 600;
double aspectRatio = (double) height / image.getHeight();
int width = (int) (image.getWidth() * aspectRatio);
return Bitmap.createScaledBitmap(image, width, height, true);
}
private void saveImageLocation(String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("image", value);
editor.commit();
}
private boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
private boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
private File getOutputMediaFile() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile = new File(getAlbumStorageDir("companyname") + File.separator
+ "IMG_" + timeStamp + "_" + phoneid + ".jpg");
return mediaFile;
}
private File getAlbumStorageDir(String albumName) {
File path = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, albumName);
file.mkdirs();
return file;
}
尝试使用库来调整图像大小和旋转图像
在异步任务中
doInBackground写下以下行
Bitmap bitmap = Picasso.with(context).load(file)//
.resize(100,100).centerInside().get();
如果图像的方向写入exif数据中,毕加索将自动旋转图像
centerInside()
将确保纵横比保持正确
现在您可以发送图像了