Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Jpeg saved from camera looks corrupted
    primarykey
    data
    text
    <p>I'm writing an Android application that saves a JPEG snapshot from the camera when the user clicks a button. Unfortunately, when I look at the JPEG file my code is saving looks corrupted. It appears to be caused by my call to parameters.setPreviewSize (see code snippet below) - if I remove that then the image saves fine; however without it I can't set the preview size and setDisplayOrientation also appears to have no effect without it.</p> <p>My app is targeting API Level 8 (Android 2.2), and I'm debugging on an HTC Desire HD. Not quite sure what I'm doing wrong here... any help would be very much appreciated!</p> <p>Cheers, Scottie</p> <pre><code>public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // Now that the size is known, set up the camera parameters and begin // the preview. Camera.Parameters parameters = mCamera.getParameters(); Camera.Size size = getBestPreviewSize(w,h); // This next call is required in order for preview size to be set and // setDisplayOrientation to take effect... // Unfortunately it's also causing JPEG to be created wrong parameters.setPreviewSize(size.width, size.height); parameters.setPictureFormat(ImageFormat.JPEG); mCamera.setParameters(parameters); mCamera.setDisplayOrientation(90); mCamera.startPreview(); } // This is the snapshot button event handler public void onSnapshotButtonClick(View target) { //void android.hardware.Camera.takePicture(ShutterCallback shutter, // PictureCallback raw, PictureCallback jpeg) mPreview.mCamera.takePicture(null, null, mPictureCallback); } // This saves the camera snapshot as a JPEG file on the SD card Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { if (imageData != null) { FileOutputStream outStream = null; try { String myJpgPath = String.format( "/sdcard/%d.jpg", System.currentTimeMillis()); outStream = new FileOutputStream(myJpgPath); outStream.write(imageData); outStream.close(); Log.d("TestApp", "onPictureTaken - wrote bytes: " + imageData.length); c.startPreview(); Toast.makeText(getApplicationContext(), String.format("%s written", myJpgPath), Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } } } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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