-1

I have problem about capturing image on Android via camera. I show my source of my Android app below.

public class RegisterActivity extends AppCompatActivity {
   private Kelolajson json;
   private Button btnfoto;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());

      ......
      json = new KelolaJson();
      btnfoto = (Button) findViewById(R.id.fotobtn);
      btnfoto.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            if (isDeviceSupportCamera()) {
               f_foto =txthp.getText().toString() +".jpg";
               fname= txthp.getText().toString();
               captureImage();
            }
            else {
               Toast.makeText(getApplicationContext(),"Your Mobile don't support camera",Toast.LENGTH_LONG).show();
            }
         }
      });
      ......
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
         if (resultCode == RESULT_OK) {
            // successfully captured the image display it in image view
            String root = Environment.getExternalStorageDirectory().toString();
            String path = root + "/Pictures/myapp";
            gmbpath = fileUri.getPath().toString();

            Bitmap tmp = BitmapFactory.decodeFile(gmbpath);
            imgpreview.setImageBitmap(tmp);

         } else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplicationContext(),
                        "You has been canceled capture.",
                        Toast.LENGTH_SHORT).show();
         } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                        "Sorry, Can\'t get photo",
                        Toast.LENGTH_SHORT).show();
          }
       }
   }

   public Uri getOutputMediaFileUri(int type) {
       return Uri.fromFile(getOutputMediaFile(type));
   }

   private static File getOutputMediaFile(int type) {

       //External sdcard location
       File mediaStorageDir = new File(
      Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "myapp");   

       // Create the storage directory if it does not exist
       if (!mediaStorageDir.exists()) {
           if (!mediaStorageDir.mkdirs()) {
               return null;
           }
       }

       // Create a media file name
       String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
       File mediaFile = null;
       if (type == MEDIA_TYPE_IMAGE) {
           fname = "_" + timeStamp;
           f_foto = tmp_hp + ".jpg";
           mediaFile = new File(mediaStorageDir.getPath() + File.separator + f_foto);


       } else {
           mediaFile= null;
       }

       return mediaFile;
   }

   private void captureImage() {
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

       fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
       intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
       // start the image capture Intent
       startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
   }
}

This source code has been running well on mobile Smartphone. But some smartphone's result of captured photo always changed orientation. For example, I capture image by smartphone portrait orientation but result of smartphone capture image is landscape orientation. Also, if landscape orientation, it is result changed to portrait orientation. This result don't follow smartphone orientation.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Can you share code for `captureImage();` method ? – adityakamble49 Jul 18 '18 at 09:09
  • private void captureImage() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // start the image capture Intent startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); } – Abdul Salam Jul 18 '18 at 09:11
  • Please update the code in question itself. Its not recommended in comments – adityakamble49 Jul 18 '18 at 09:12
  • Have a look on this link https://stackoverflow.com/questions/49232265/how-can-i-specify-the-taken-image-quality-on-android/49232371#49232371 it might help you – kishna.147 Jul 18 '18 at 09:13

2 Answers2

0

Checks for orientation associated with URI of the captured image from the camera.

  try {
         getContentResolver().notifyChange(imageUri, null);
         File imageFile = new File(imagePath);
         ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
         int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL);

          switch (orientation) {
          case ExifInterface.ORIENTATION_ROTATE_270:
          rotate = 270;
          break;
          case ExifInterface.ORIENTATION_ROTATE_180:
          rotate = 180;
          break;
          case ExifInterface.ORIENTATION_ROTATE_90:
          rotate = 90;
          break;
        }
          Log.v(Common.TAG, "Exif orientation: " + orientation);
        } catch (Exception e) {
          e.printStackTrace();
        }

         Matrix matrix = new Matrix();
         matrix.postRotate(orientation);
         Bitmap cropped = Bitmap.createBitmap(scaled, x, y, width, height, matrix, true);

Apparently Samsung phones set the EXIF orientation tag, rather than rotating individual pixels.Reading the Bitmap using BitmapFactory does not support this tag.What i found the solution to this problem was using ExifInterface in onActivityResult method of the activity.

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
0

Try this code:

   private static final int CAMERA_PIC_REQUEST = 1337;

    Button btnn;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnn = (Button) findViewById(R.id.btn);
        imageView = (ImageView) findViewById(R.id.Img);
        sf = getSharedPreferences("photo", Context.MODE_PRIVATE);
        String de = sf.getString("image", "");
        byte[] c = Base64.decode(de, Base64.DEFAULT);
        Bitmap bb = BitmapFactory.decodeByteArray(c, 0, c.length);
        imageView.setImageBitmap(bb);


        btnn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, CAMERA_PIC_REQUEST);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK && null != data) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            byte[] b = bytes.toByteArray();
            String img = Base64.encodeToString(b, Base64.DEFAULT);
            SharedPreferences.Editor editor = sf.edit();
            editor.putString("image", img);
            editor.commit();


            imageView.setImageBitmap(thumbnail);

        }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15