Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid How to size and save camera picture as bitmap for display in fixed rect area within activity
    text
    copied!<p>I have an app that takes pictures of items, and these items must be viewable in a certain fixed region on the ActivityScreen. The problem is the picture displayed in the activity from the saved bitmap, looks nothing like what I previewed and photographed. It looks zoomed in and shows areas I did not even see in the preview when I took the picture: Here is the key code lines </p> <p><strong>IMAGE CAPTURE</strong>:</p> <pre><code>surfHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); captImageView = (ImageView)findViewById(R.id.CamImageView); byte [] photoData; Camera cam; SurfaceHolder surfHolder; SurfaceView surfView; Camera.PictureCallback cback; cback = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { final int length = data.length; opts.inSampleSize = 4; final BitmapFactory.Options opts = new BitmapFactory.Options(); Bitmap bMap = null; try { bMap = BitmapFactory.decodeByteArray(data, 0, length, opts); } catch(Exception e) { }catch(OutOfMemoryError e) { } captImageView.setImageBitmap(bitmap); if(bMap==null) { cam.startPreview(); } else { savePhotoButton.setVisibility(ImageView.VISIBLE); takePhotoButton.setVisibility(ImageView.INVISIBLE); photoData = data; } } } </code></pre> <p><strong>IMAGE SAVE</strong>:</p> <pre><code>captImageView.setImageBitmap(null); // SAVE THE PICTURE THIS SAVES PICTURE IN WRONG SIZE. LOOKS LIKED ZOOMED IN NOT WHAT WAS PREVIEWED!!! public void photoSave(byte[] data) { final int length = data.length; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; try { Bitmap bMap = BitmapFactory.decodeByteArray(data, 0, length, options); int quality = 75; File file = new File(getDir(), fname); fileOutputStream = new FileOutputStream(file); bMap.compress(CompressFormat.JPEG, quality, fileOutputStream); }catch(FileNotFoundException e) { } catch(OutOfMemoryError e) {} } </code></pre> <p>IMAGE DISPLAY as DRAWABLE in ACTIVITY DOES NOT WORK SHOWS IMAGE AS ZOOMED OR PORTIONS NOT SEEN IN PREVIEW AT ALL WHEN PICTURE WAS TAKEN!!</p> <pre><code>BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = inSampSize; Bitmap bitmap = BitmapFactory.decodeFile(path, options); Drawable drawable = bitmap; ImageView pictureView = (ImageView)findViewById(R.id.pictureViewer); pictureView.setImageDrawable(drawable); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload