Android Camera Height and Width [duplicate] - android

This question already has answers here:
Android Camera Intent: how to get full sized photo?
(8 answers)
Closed 5 years ago.
I am currently doing a fault reporting page whereby the end user is able to report an image either by capturing using the camera or selecting a photo from the gallery. I am not sure why but when the image is taken using the camera, the height and width returned is about 220 by 180 which is a very blur image. I have tried looking online for other tutorials but my code seems to be the same as the others. Not too sure if it is my phone or what.
My code for Camera Intent:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
My code after Capturing image from Camera:
thumbnail = (Bitmap) data.getExtras().get("data");
bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
ivImage.setVisibility(View.VISIBLE);
ivImage.setImageBitmap(thumbnail);
I've tried getting the width and height before and after compression, both returns the same value.

May be you should not compress the image rather simply use BitmapFactory.decodeFile(filePath) or any suitable method for getting your image from file into a bitmap (if not done already) and then set the bitmap object to the ImageResource like this:
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
And don't simply type cast the data received from intent. As far as I know about camera activity class, it will return a URI of the captured image. Try to receive it in onActivityResult() like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
if (requestCode == REQUEST_CAMERA) {
Uri photoUri = data.getData();
// Calculate the bitmap here according to the width of the device
((ImageView) findViewById(R.id.captured_image)).setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}

Related

Android change the pixel or the image size when saving to SQLITE

I know this is quite common to ask but I'm really confused this time. All I need is to save the BLOB image to the SQLite and I have already done and saved it to SQLite, actually, I have two images saved as BLOB in SQLite I compared them but I really don't know the difference the other one contains 3000X4000 pixels 2.72 MIB which much heavier size and the other one is much lighter which is 182x250 50.81Kib and this is what I really want the lightier size. I upload two images to see what's really happening
Actually, I've been trying to develop Text Image Recognition. The image which bigger size sample, after capturing an image it should be crop and saves to SQLite while the lesser size just directly captured image and save to SQLite now they have the save function to save to SQLite I really don't know why they have different size and I think the problem is on the crop or Pickcamera()method which it changes the size of the image? is that possible?.
The bigger one
The lesser one
The bigger size code Image after clicking button it goes to pickCamera() method and the image display to the another activity
public void pickCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "NewPic");
values.put(MediaStore.Images.Media.DESCRIPTION, "Image to Text");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
Intent cameraIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(cameraIntent, IMAGE_PICK_CAMERA_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
if (requestCode == IMAGE_PICK_GALLER_CODE){
CropImage.activity(data.getData()).setGuidelines(CropImageView.Guidelines.ON).start(this);
}
if (requestCode == IMAGE_PICK_CAMERA_CODE){
CropImage.activity(image_uri).setGuidelines(CropImageView.Guidelines.ON).start(this);
}
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode ==RESULT_OK){
Uri resultUri = result.getUri();
resultUri.getPath();
BitmapDrawable bitmapDrawable = (BitmapDrawable)mPreviewIv.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
TextRecognizer recognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if(!recognizer.isOperational()){
Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show();
}
else{
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> items = recognizer.detect(frame);
StringBuilder sb = new StringBuilder();
for (int i = 0; i<items.size(); i++){
TextBlock myItem = items.valueAt(i);
sb.append(myItem.getValue());
sb.append("\n");
}
Intent i = new Intent(ScanCashCard.this, ScannedDetails.class);
//camera I want to display the image view to another Activity and save to SQLite
i.putExtra("CashCardImage",image_uri.toString());
startActivity(i);
}
}
}
}
Display to another activity
String resultUri = extras.getString("CashCardImage");
Uri myUri = Uri.parse(resultUri);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),myUri);
mPreviewCashCard.setImageBitmap(bitmap);
This code is from the lesser size
public void pickcamera(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 101);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101){
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
mPreview4PsId.setImageBitmap(bitmap);
}
}
I think the problem is on the pickCamera() method which they different but when I follow the code from the lesser size the crop will not be available anymore, I stuck with this module anyone can help me? this is really help me a lot
So I just add a bunch of code which set and replace the default resolution of the Image pixel because I guess the default Pixel of the Image resolution from camera is 3000x4000 pixel so you just need to set the width and height of the ImageView. So I just set 187x250
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),myUri);
mPreviewCashCard.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 187, 250, false));

Android Camera Photo Thumbnail Orientation

We have been using a bunch of code that uses the camera with the desired end result, but I want to get to the bottom of this with clean code. I'm simply following the Android docs here verbatim, and getting a rotated thumbnail. Below is the code, please find the working project in this branch of my Bitbucket repository.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ImageView view = (ImageView) findViewById(R.id.imageView);
view.setImageBitmap(imageBitmap);
}
}
I know that this is just the thumbnail. But it seems the thumbnail is completely useless, unless you get the full file and read the exif info from that.
I know that StackOverflow says "get the exif rotation from the full image file and rotate the actual bitmap before recompressing it into another jpg file". But isn't this a little too much unnecessary computation? What good is the thumbnail if it's useless by itself without getting the orientation from the full file?
Am I missing something?

Android capture and use image not working

I've written code in Android to capture an image and then use that image as follows:
private static Intent i;
final static int cons = 0;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public void takePicture()
{
i= new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //Open camera
startActivityForResult(i, cons);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Log.d("Testing if condition", "Test"); //This code does not execute
Bundle ext = data.getExtras();
Log.d("Upload image", ext.toString());
sendForUpload(data); // function to upload file to server
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
This code lets me take a photo and it saves to the SD card. However, I'm not sure if the file is sent to the sendForUpload function where I've handled getting the path and uploading the file. In fact nothing inside the if (resultCode == RESULT_OK) block works. How do I use the image captured from this activity?
Well, you have a few problems.
First, you are calling startActivityForResult(i, cons);. cons is 0. You are trying to compare 0 with CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE. Since CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is 100, that will not work.
Second, you are calling data.getExtras().toString() (split over two Java statements). I would expect this to return a value like android.os.Bundle#34334143. If that is what you want to upload, fine. I am guessing that you want to upload the image. Since you did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, you get a Bitmap of the thumbnail image taken by the camera via data.getParcelableExtra("data").
Third, you are making inappropriate assumptions:
This code lets me take a photo and it saves to the SD card.
There is no requirement for the camera app to save the image anywhere, let alone to removable storage. In fact, I would argue that this is a bug in your camera app. That is not surprising, as ACTION_IMAGE_CAPTURE implementations have bugs.
where I've handled getting the path and uploading the file
There is no path. You did not include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, and so all you get back is the thumbnail Bitmap.
I'm using this to put the image in a ImageView:
Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
You can use bitmap or uri objects for upload or show your picture..
Hope it helps you.

pick image from gallery and convert into byte data how?

I need to pick an image from gallery and then convert it into byte data. I know how to pick image from gallery. Also I know how to convert image to byte data. But problem is i convert image that are in drawable but now I need to pick it from gallery and convert it to byte code. Any help
THanks
In onClick function I am using this code to pick image from gallery
Intent image = new Intent(Intent.ACTION_GET_CONTENT);
image.setType("Image/*");
startActivityForResult(image, 0);
And I have used following code to convert image that is in drawable to byte data.
bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
data = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40 , data);
bitmapdata = data.toByteArray();
Now how would i convert image from gallery to byte data.
Thanks
In onActivityResult you will receive the Uri to your selected image like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null){
Uri imageUri = data.getData();
//....
}
}
Then to retrieve it from the MediaStore you should use :
Bitmap bitmap =
MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
after that, you should process the Bitmap like you do it now.

Android: Not able to get original photo captured by camera (Able to read compress photo only)

In my application I capture photo by android camera and then I want to send it to the server. For this I use Client Socket programming. I convert the capture photo into bytearray(byte[]) and then send it to the server. Every thing work perfactally.
Problem is there I am not able to send original photo to the server. Thumbnail photo is sended by the android mobile phone. But when I capture photo by the camera then original photo is there in Gallery.
How to get this original photo to use in my application?
My Code:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG);
cameraIntent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(cameraIntent, "Select Picture"),
CAMERA_REQUEST);
And in onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// byte[] a = (byte[]) data.getExtras().getByteArray("data"); // I also use this but not work
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
}
}
in bitmapdata photo is there but compressed photo not the original.
Some of people say that, change quality field of compress(CompressFormat.JPEG, 100, bos); in between 0 to 100 but nothing will happen.
-- >> is there any other way also to get original photo which is captured by the camera
-- >> When The photo is in folder of my computer then I read this photo in the file by giving the path. ex- File file = new File(c:\newphoto\image.jpg); . can I use this code in android to read original photo because I know the name and location of photo. If yes then what is the path to read photo in gallery. Is this work if I give path as: \DCIM\Camera\photoName.jpg.
-- >> Or some change need in my current code and it will work fine?
You are using the Intent to capture the image using ACTION_IMAGE_CAPTURE. If you normally starts your camera using Intent then it will return image as a bitmap in onActivityResult() but it will be for thumbnail purpose.
If you want to get full resolution image from the camera then you should provide a file with the intent which you are firing to start the camera activity.
You can do like below
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, imagename);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);
Here there is a MediaStore.EXTRA_OUTPUT parameter which takes a Uri of the file in which you want your camera to write a images.
For more information you can refer below example
Capture full resolution image from camera

Categories

Resources