Setting ACTION_IMAGE_CAPTURE picture size.

Put your problem here if it does not fit any of the other categories.

Setting ACTION_IMAGE_CAPTURE picture size.

Postby hce » Sun Jan 22, 2012 4:43 am

Hi again,

I am using

Code: Select all
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
               intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
               startActivityForResult(intent, TAKE_PHOTO_CODE);


The native image size for the phone I am programming for is 1600x900. I need to somehow change that to a 1024x768 size.

Is there any way to do this?
hce
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jan 18, 2012 4:38 am

Top

Re: Setting ACTION_IMAGE_CAPTURE picture size.

Postby Phyll » Sun Jan 22, 2012 6:34 pm

Hi hce,

If I'm understanding your question. You take an image with the built-in camera and the bitmap you get back is bigger than you want? So you want to make it smaller?

I think you should be able to create a smaller bitmap and use drawBitmap to copy that one to it which can reduce it in size. Does that sound like what you want?

I don't think there is any way to set that size in for the camera before you take a picture. And it may not be universal if one did have that feature.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Setting ACTION_IMAGE_CAPTURE picture size.

Postby hce » Sun Jan 29, 2012 11:26 pm

Yes. I am taking a picture with the camera, then sending the uri location back to my app. I am then emailing the photos off to a server. The problem is that when I take the photo from inside of my app with the camera there is no size option, so it takes the picture at 8MP and the photo comes out to being from 2MB to 4MB. If I take 10 photos and try to email them it comes out to be a 30MB to 40MB and they never get sent..

Also the pictures have to be in a jpeg or jpg format for my server to process them correctly. Is there a way to compress the jpeg.
hce
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jan 18, 2012 4:38 am

Re: Setting ACTION_IMAGE_CAPTURE picture size.

Postby Phyll » Mon Jan 30, 2012 4:41 am

Hi hce,

Here's a way to put the image data from the camera as bytes into a file. As you can see, it sets the jpeg compression (quality) and also reduces the sample size from the input array of bytes (inSampleSize). This will produce a much smaller file to transfer.

Code: Select all
      public boolean StoreByteImage(byte[] imageData,int quality) {
         try {
              File sdImageMainDirectory = new File(maindirectory);
              FileOutputStream fileOutputStream = null;
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 2; 
            Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                  imageData.length,options);
            fileOutputStream = new FileOutputStream(
                  sdImageMainDirectory.toString() +"/"+imagefile);
            BufferedOutputStream bos = new BufferedOutputStream(   
                  fileOutputStream);
            myImage.compress(CompressFormat.JPEG, quality, bos);
            bos.flush();
            bos.close();
          }//create data file
          catch (FileNotFoundException e)
          {
             Log.e("DATAFILE", "File Not Found Error");
             e.printStackTrace();
             return false;
          }//data file error
          catch (IOException e)
          {
             Log.e("DATAFILE", "IOException Error");
             e.printStackTrace();
             return false;
          }//data file error
          return true;
       }//storebyteimage 


Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Setting ACTION_IMAGE_CAPTURE picture size.

Postby hce » Mon Jan 30, 2012 7:11 am

Thank you I will give it a try.
hce
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jan 18, 2012 4:38 am

Re: Setting ACTION_IMAGE_CAPTURE picture size.

Postby hce » Mon Jan 30, 2012 3:38 pm

One quick question. This is where the java gets me, what do i need to do to pass the image into the method? Do i need to turn the uri address into a byte[]?
hce
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jan 18, 2012 4:38 am

Top

Re: Setting ACTION_IMAGE_CAPTURE picture size.

Postby Phyll » Mon Jan 30, 2012 5:37 pm

Hi hce,

I think all this came from a picture taking example originally. I've had it for a long time and it was modified a long time ago. But the callback for the camera device in android sends back a byte array as shown here:

Code: Select all
   Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
      public void onPictureTaken(byte[] imageData, Camera c) {
         if (imageData != null) {
            Intent mIntent = new Intent();
            Snap(mSurfaceView, imageData);
            mCamera.startPreview();
            setResult(PICTURE_MODE, mIntent);
               }//if
          }//onpicturetaken
       };//picturecallback


The Snap method above looks like this:

Code: Select all
    public void Snap(View view, byte[] data) {
        dialog = new ProgressDialog(this);
        dialog.setCancelable(false);
        dialog.setMessage("Saving");
        dialog.show();
        saving=true;
        if (StoreByteImage(data, 80)) {
            //Sending file thread
            Thread background = new Thread (new Runnable() {
                public void run() {
                    int retval=SendFile(CamaraView.this);
                    Log.e("DIALOG", "Dismissed");
                    dialog.dismiss();
                     Message m = resultHandler.obtainMessage();
                    m.arg1=retval;
                   resultHandler.sendMessage(m);
                    }//run
                });//thread statements block
            background.start();
            //thread started continue with main program
            }//if saved locally
            else {
               Toast.makeText(mContext, "File Error", Toast.LENGTH_LONG).show();
            if (TimerReset) TimerActive = true;
            TimerReset = false;
            saving=false;
                }//reset everything important     
        }//snap


You'll see that the Snap method just passes the byte image on to the storage routine before calling a background thread that sends out the file to the internet.

Can't say whether or not this is the best practice but I would say, if you don't have it in a byte array already you'll probably have to make it that way. Now how do we do that?

Here is some code that takes a bitmap from a file and makes it into a byte array:

Code: Select all
        FileInputStream filein =new FileInputStream("mybitmap");
        Bitmap bitmap = BitmapFactory.decodeStream(filein);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, quality, bos);
        byte[] imageData = bos.toByteArray();


Perhaps something here can help.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Setting ACTION_IMAGE_CAPTURE picture size.

Postby hce » Thu Feb 02, 2012 7:12 am

I think I have got it going I am using

Code: Select all

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
            startActivityForResult(intent, TAKE_PHOTO_CODE);


To take the picture then I am using

private Uri getImageUri() {

Random generator = new Random();
int random = generator.nextInt(50000) + 1;

String capture_title = Integer.toString(random);

String STORAGE_FILE= "FILENAME";
String collected = null;
try {
FileInputStream fis = openFileInput(STORAGE_FILE);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1) {
collected = new String(dataArray);
}
fis.close();
} catch (FileNotFoundException e) {
Log.e("DATAFILE", "STORAGE_FILEFile Not Found Error");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File(Environment.getExternalStorageDirectory()
+ "/dcim/" + collected, capture_title + ".jpeg");

//I am using this to set a textbox so the user knows the location of the photo
Uri imgUri = Uri.fromFile(file);

holder = imgUri.toString();

editText.setText(holder);

}

return imgUri;
}



I know this will take and store an original photo then one that is resized but that is fine, the photo taking process is working fine for know and the user can use that file as a backup incase of a problem with the resized photo. This is how I am resizing.

Code: Select all
                               stringEx = editText.getText().toString();
            
            if (stringEx.startsWith("file://")) {
               ten = ten.substring(11);
            }
            
            
            Bitmap mBitmap = BitmapFactory.decodeFile(stringEx );                 
       
               
                Display display = getWindowManager().getDefaultDisplay();
                int newHeight = display.getHeight();
                int newWidth = display.getWidth();
                Bitmap resized = Bitmap.createScaledBitmap(mBitmap, newHeight, newWidth, false);                   
 
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                resized.compress(CompressFormat.JPEG, 0 /*ignored for PNG*/, bos);
                byte[] bitmapdata = bos.toByteArray();

                StoreByteImage(bitmapdata, 100);



Then I can send it into the StoryByteImage function.

I have to do some playing around with it to get it optimized, but seems to work ok for know. Let me know if you have any input. Thanks for the help also

hce
hce
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Jan 18, 2012 4:38 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 11 guests