1. Download our Official Android App: Forums for Android!

Android Camera Code problem....urgent!!!!!

Discussion in 'Introductions' started by Shinchan, Jul 13, 2011.

  1. Shinchan

    Shinchan Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    3
    Joined:
    Jul 13, 2011

    Jul 13, 2011
    3
    0
    25
    Helllo frnds.I am creating android application which uses camera code.I have used android-API 2.1 . I have installed my application on dell mobile and the orientation of camera is correct.However, i installed my application in other android mobiles like samsung galaxy s2 etc.Here,the problem is with camera orientation , even in portrait mode the camera is rotated 90 degrees.Can any1 suggest me if any camera parameteres need to be changed so that camera orientation works well on all mobiles...Here is my code:


    //class CameraDemo
    public class CameraDemo extends Activity {
    private static final String TAG = "CameraDemo";
    Camera camera;
    Preview preview;
    Button buttonClick;
    Button next;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    try
    {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.camdemo);

    preview = new Preview(this);
    ((FrameLayout) findViewById(R.id.preview)).addView(preview);

    buttonClick = (Button) findViewById(R.id.buttonClick);
    next = (Button) findViewById(R.id.next);
    buttonClick.setOnClickListener( new OnClickListener() {
    public void onClick(View v) {

    preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);

    try{

    Constants.takepic=1;

    buttonClick.setEnabled(false); //once click is clicked, its disable
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }


    });
    next.setOnClickListener( new OnClickListener() {
    public void onClick(View v) {
    //preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
    if(Constants.takepic==1)
    {
    Intent intent_new=new Intent(CameraDemo.this,NotesAndUpload.class);
    startActivity(intent_new);
    }
    else
    {
    Toast.makeText(CameraDemo.this, "Take a picture",5);
    }

    }


    });

    Log.d(TAG, "onCreate'd");
    }
    catch(Exception e ){}
    }


    ShutterCallback shutterCallback = new ShutterCallback() {
    public void onShutter() {
    Log.d(TAG, "onShutter'd");
    }
    };

    /** Handles data for raw picture */
    PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
    Log.d(TAG, "onPictureTaken - raw");
    }
    };

    /** Handles data for jpeg picture */
    PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
    //taking time and date
    Constants.dateValue=dateAndTime("yyyy-MM-dd");
    Constants.timeValue=dateAndTime("HH:mm:ss");
    FileOutputStream outStream = null;
    try {
    // write to local sandbox file system
    // outStream =
    CameraDemo.this.openFileOutput(String.format("%d.jpg",
    System.currentTimeMillis()), 0);
    // Or write to sdcard
    long imageNameLong=System.currentTimeMillis();
    Constants.imageName=new Long(imageNameLong).toString();
    outStream = new
    FileOutputStream(String.format("/sdcard/%d.jpg",imageNameLong));
    outStream.write(data);
    Constants.takepic=1;
    outStream.close();

    Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    }
    Log.d(TAG, "onPictureTaken - jpeg");
    }
    };

    }


    //Preview Class
    class Preview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "Preview";

    SurfaceHolder mHolder;
    public Camera camera;

    Preview(Context context) {
    super(context);

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, acquire the camera and tell it where
    // to draw.
    camera = Camera.open();
    try {
    camera.setPreviewDisplay(holder);


    camera.setPreviewCallback(new PreviewCallback() {

    public void onPreviewFrame(byte[] data, Camera arg1) {
    /*FileOutputStream outStream = null;
    try {
    outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",
    System.currentTimeMillis()));
    outStream.write(data);
    outStream.close();
    Log.d(TAG, "onPreviewFrame - wrote bytes: " + data.length);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    }*/
    Preview.this.invalidate();
    }
    });
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    // Surface will be destroyed when we return, so stop the preview.
    // Because the CameraDevice object is not a shared resource, it's very
    // important to release it when the activity is paused.
    camera.stopPreview();
    camera = null;
    }

    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 NEED TO BE CHANGED :-
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(w, h);
    if(getResources().getConfiguration().orientation ==
    Configuration.ORIENTATION_PORTRAIT)
    {
    parameters.set("rotation",90);
    parameters.set("orientation", "portrait");
    }
    if (getResources().getConfiguration().orientation ==
    Configuration.ORIENTATION_LANDSCAPE)
    {
    parameters.set("rotation", 90);
    parameters.set("orientation", "landscape");

    }
    parameters.set("jpeg-quality", 50);
    camera.setParameters(parameters);
    camera.startPreview();
    }

    @Override
    public void draw(Canvas canvas) {
    super.draw(canvas);
    Paint p= new Paint(Color.RED);
    Log.d(TAG,"draw");
    canvas.drawText("PREVIEW", canvas.getWidth()/2, canvas.getHeight()/2, p );
    }
    }
     

    Advertisement

  2. SamuraiBigEd

    SamuraiBigEd Under paid Sasquatch!
    Rank:
    None
    Points:
    633
    Posts:
    4,181
    Joined:
    Nov 8, 2009

    Nov 8, 2009
    4,181
    2,088
    633
    Male
    Production, Warehouse & Installation Manager
    San Antonio, Texas
  3. Martimus

    Martimus One bite at a time...
    Rank:
    None
    Points:
    843
    Posts:
    19,535
    Joined:
    Jul 9, 2010

    Jul 9, 2010
    19,535
    4,045
    843
    Male
    Engineer
    'neath a cactus
    Welcome to the Android Forums!

    The forum that you posted in is specifically for Introductions. It's highly unlikely that you are going to find programming assistance in this particular forum. Please re-post your question in the Android Developers forum at the link provided above...
     

Share This Page

Loading...