在android上保存位图时出现问题

sycxhyv7  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(499)

我在android上做了一个小应用,为照片添加时间戳,我有一个小问题

  1. private void openCamera() {
  2. ContentValues values = new ContentValues();
  3. values.put(MediaStore.Images.Media.TITLE, "New picture");
  4. values.put(MediaStore.Images.Media.DESCRIPTION, "New picture");
  5. uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , values);
  6. Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  7. cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT , uri);
  8. startActivityForResult(cameraIntent , IMAGE_CAPTURE_CODE);
  9. }

当我点击按钮时,会调用opencamera。
一切都很好,照片保存在我的手机上,照片在我的imageview上

  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  3. super.onActivityResult(requestCode, resultCode, data);
  4. if (resultCode == RESULT_OK) {
  5. imageView.setImageURI(uri);
  6. Bitmap src = null;
  7. try {
  8. src = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }
  12. Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
  13. SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:MM:ss");
  14. String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system
  15. Canvas cs = new Canvas(dest);
  16. Paint tPaint = new Paint();
  17. tPaint.setTextSize(100);
  18. tPaint.setColor(Color.BLUE);
  19. tPaint.setStyle(Paint.Style.FILL);
  20. // image qui est tourné , a changer et fini
  21. cs.drawBitmap(src, 0f, 0f, null);
  22. float height = tPaint.measureText("yY");
  23. cs.drawText(dateTime, 20f, height+15f, tPaint);
  24. try {
  25. dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/timeStampedImage.jpg")));
  26. } catch (FileNotFoundException e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. }
  30. }
  31. }

之后,在activityresult上,我想将时间戳添加到照片中。
保存图片时,除照片外,其他所有功能均正常:(
我不知道为什么,在代码上,旋转被修改了?
非常感谢您的帮助:)
例如:之前https://ibb.co/1nltjx5 之后https://ibb.co/l1mg1fj
新尝试

  1. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  2. super.onActivityResult(requestCode, resultCode, data);
  3. if (resultCode == RESULT_OK) {
  4. imageView.setImageURI(uri);
  5. Bitmap src = null;
  6. try {
  7. src = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
  8. } catch (IOException e) {
  9. e.printStackTrace();
  10. }
  11. ExifInterface ei = null;
  12. try (InputStream inputStream = MainActivity.this.getContentResolver().openInputStream(uri)){
  13. ei = new ExifInterface(inputStream);
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
  18. ExifInterface.ORIENTATION_UNDEFINED);
  19. Bitmap rotatedBitmap = null;
  20. switch(orientation) {
  21. case ExifInterface.ORIENTATION_ROTATE_90:
  22. rotatedBitmap = rotateImage(src, 90);
  23. break;
  24. case ExifInterface.ORIENTATION_ROTATE_180:
  25. rotatedBitmap = rotateImage(src, 180);
  26. break;
  27. case ExifInterface.ORIENTATION_ROTATE_270:
  28. rotatedBitmap = rotateImage(src, 270);
  29. break;
  30. case ExifInterface.ORIENTATION_NORMAL:
  31. default:
  32. rotatedBitmap = src;
  33. }
  34. SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:MM:ss");
  35. String dateTime = sdf.format(Calendar.getInstance().getTime()); // reading local time in the system
  36. Canvas cs = new Canvas(rotatedBitmap);
  37. Paint tPaint = new Paint();
  38. tPaint.setTextSize(100);
  39. tPaint.setColor(Color.BLUE);
  40. tPaint.setStyle(Paint.Style.FILL);
  41. // image qui est tourné , a changer et fini
  42. cs.drawBitmap(src, 0f, 0f, null);
  43. float height = tPaint.measureText("yY");
  44. cs.drawText(dateTime, 20f, height+15f, tPaint);
  45. try {
  46. rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/timeStampedImage.jpg")));
  47. } catch (FileNotFoundException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51. }
  52. }
  53. public static Bitmap rotateImage(Bitmap source, float angle) {
  54. Matrix matrix = new Matrix();
  55. matrix.postRotate(angle);
  56. return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
  57. matrix, true);
  58. }

我用collabor的链接进行了修改,并创建了一个带有uri源方向的位图
现在结果。。。图片分为两部分(好方向和坏方向):(https://ibb.co/c9495pl

aiqt4smr

aiqt4smr1#

问题已解决
我用这个密码

  1. Bitmap src = null;
  2. try {
  3. src = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
  4. } catch (IOException e) {
  5. e.printStackTrace();
  6. }
  7. ExifInterface ei = null;
  8. try (InputStream inputStream = MainActivity.this.getContentResolver().openInputStream(uri)){
  9. ei = new ExifInterface(inputStream);
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
  14. ExifInterface.ORIENTATION_UNDEFINED);
  15. Bitmap rotatedBitmap = null;
  16. switch(orientation) {
  17. case ExifInterface.ORIENTATION_ROTATE_90:
  18. rotatedBitmap = rotateImage(src, 90);
  19. break;
  20. case ExifInterface.ORIENTATION_ROTATE_180:
  21. rotatedBitmap = rotateImage(src, 180);
  22. break;
  23. case ExifInterface.ORIENTATION_ROTATE_270:
  24. rotatedBitmap = rotateImage(src, 270);
  25. break;
  26. case ExifInterface.ORIENTATION_NORMAL:
  27. default:
  28. rotatedBitmap = src;
  29. }
  30. Uri uriTest = getImageUri( rotatedBitmap);
  31. Bitmap dest = Bitmap.createBitmap(rotatedBitmap.getWidth(), rotatedBitmap.getHeight(), Bitmap.Config.ARGB_8888);
  32. public Uri getImageUri( Bitmap inImage) {
  33. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  34. inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  35. double random = Math.random() * 1000 + 1;
  36. String path = MediaStore.Images.Media.insertImage(getContentResolver(), inImage, "picture" + random, null);
  37. return Uri.parse(path);
  38. }

我创建一个位图“dest”与位图旋转!

  1. Bitmap dest = Bitmap.createBitmap(rotatedBitmap.getWidth(), rotatedBitmap.getHeight(), Bitmap.Config.ARGB_8888);

  1. Canvas cs = new Canvas(dest);

并在旋转位图上绘制位图

  1. cs.drawBitmap(rotatedBitmap, 0f, 0f, null);

为了拯救它

  1. MediaStore.Images.Media.insertImage(getContentResolver() , dest , namePhoto , "Heure sur la photo");
展开查看全部

相关问题