[英]Issue when saving Bitmap on Android
我在 Android 上做了一个小应用程序来为照片添加时间戳,但我有一个小问题
private void openCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "New picture");
uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT , uri);
startActivityForResult(cameraIntent , IMAGE_CAPTURE_CODE);
}
当我单击按钮时,会调用 openCamera。
一切都很好,照片保存在我的手机上,照片在我的 imageView 上
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
imageView.setImageURI(uri);
Bitmap src = null;
try {
src = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:MM:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system
Canvas cs = new Canvas(dest);
Paint tPaint = new Paint();
tPaint.setTextSize(100);
tPaint.setColor(Color.BLUE);
tPaint.setStyle(Paint.Style.FILL);
// image qui est tourné , a changer et fini
cs.drawBitmap(src, 0f, 0f, null);
float height = tPaint.measureText("yY");
cs.drawText(dateTime, 20f, height+15f, tPaint);
try {
dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/timeStampedImage.jpg")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
之后,在 ActivityResult 上,我想将时间戳添加到照片中。
一切正常,但保存图片时照片会旋转:(
而且我不知道为什么,在代码的何处修改了旋转?
非常感谢您的帮助:)
例:前https://ibb.co/1nLtJx5后https://ibb.co/L1MG1FJ
新尝试
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
imageView.setImageURI(uri);
Bitmap src = null;
try {
src = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
ExifInterface ei = null;
try (InputStream inputStream = MainActivity.this.getContentResolver().openInputStream(uri)){
ei = new ExifInterface(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap rotatedBitmap = null;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(src, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(src, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(src, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = src;
}
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:MM:ss");
String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system
Canvas cs = new Canvas(rotatedBitmap);
Paint tPaint = new Paint();
tPaint.setTextSize(100);
tPaint.setColor(Color.BLUE);
tPaint.setStyle(Paint.Style.FILL);
// image qui est tourné , a changer et fini
cs.drawBitmap(src, 0f, 0f, null);
float height = tPaint.measureText("yY");
cs.drawText(dateTime, 20f, height+15f, tPaint);
try {
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/timeStampedImage.jpg")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}
我已经使用合作者的链接进行了修改,并创建了一个带有 Uri 源方向的位图
现在结果......图片分为两部分(方向好和方向不好):( https://ibb.co/C9495PL
问题解决了
我用这个代码
Bitmap src = null;
try {
src = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
ExifInterface ei = null;
try (InputStream inputStream = MainActivity.this.getContentResolver().openInputStream(uri)){
ei = new ExifInterface(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
Bitmap rotatedBitmap = null;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotatedBitmap = rotateImage(src, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotatedBitmap = rotateImage(src, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotatedBitmap = rotateImage(src, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
rotatedBitmap = src;
}
Uri uriTest = getImageUri( rotatedBitmap);
Bitmap dest = Bitmap.createBitmap(rotatedBitmap.getWidth(), rotatedBitmap.getHeight(), Bitmap.Config.ARGB_8888);
public Uri getImageUri( Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
double random = Math.random() * 1000 + 1;
String path = MediaStore.Images.Media.insertImage(getContentResolver(), inImage, "picture" + random, null);
return Uri.parse(path);
}
我创建了一个位图旋转的位图“dest”!
Bitmap dest = Bitmap.createBitmap(rotatedBitmap.getWidth(), rotatedBitmap.getHeight(), Bitmap.Config.ARGB_8888);
和
Canvas cs = new Canvas(dest);
并在rotatedBitmap上绘制Bitmap
cs.drawBitmap(rotatedBitmap, 0f, 0f, null);
为了保存它
MediaStore.Images.Media.insertImage(getContentResolver() , dest , namePhoto , "Heure sur la photo");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.