0
public class CameraActivity
extends Activity
implements SurfaceHolder.Callback {

    private LayoutInflater myInflater = null;
    Camera myCamera;
    byte[] tempdata;
    boolean myPreviewRunning = false;
    private SurfaceHolder mySurfaceHolder;
    private SurfaceView mySurfaceView;
    Button takePicture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_camera);

        mySurfaceView = (SurfaceView) findViewById(R.id.surface);
        if (mySurfaceHolder == null) {
            mySurfaceHolder = mySurfaceView.getHolder();
        }

        mySurfaceHolder.addCallback(this);
        mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        myInflater = LayoutInflater.from(this);
        View overView = myInflater.inflate(R.layout.second_layer_camera,null);
        this.addContentView(overView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

        takePicture = (Button) findViewById(R.id.button);
        takePicture.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myCamera.takePicture(myShutterCallback, myPictureCallback, myJpeg);
            }
        });
    }

    ShutterCallback myShutterCallback = new ShutterCallback() {
        @Override
        public void onShutter() {
        }
    };

    PictureCallback myPictureCallback = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera myCamera) {
        }
    };

    PictureCallback myJpeg = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera myCamera) {
            if(data != null){
                tempdata = data;
                done();
            }
        }
    };

    void done(){
        Bitmap bm = BitmapFactory.decodeByteArray(tempdata, 0, tempdata.length);
        String url = Images.Media.insertImage(getContentResolver(), bm, null, null);
        bm.recycle();
        Bundle bundle = new Bundle();
        if(url != null){
            bundle.putString("url",url);
            Intent mIntent = new Intent();
            mIntent.putExtras(bundle);
            setResult(RESULT_OK, mIntent);
        }
        else{
            Toast.makeText(this, "Picture can not be saved", Toast.LENGTH_SHORT).show();
        }
       // finish();
        myCamera.startPreview();
    }

       @Override
       public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      try{
          if(myPreviewRunning){
              myCamera.stopPreview();
              myPreviewRunning = false;
          }
          Camera.Parameters p = myCamera.getParameters();
          p.setPreviewSize(width,height);

          myCamera.setParameters(p);
          myCamera.setPreviewDisplay(holder);
          myCamera.startPreview();
          myPreviewRunning = true;
      }catch(Exception e){}
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        myCamera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        myCamera.stopPreview();
        myPreviewRunning = false;
        myCamera.release();
        myCamera = null;
    }
}

When launching the app, the custom camera appears rotated. I need to show a normal camera. I find the reason for it opens the wrong camera.

I did not find another way to create a customized camera.

Andreas
  • 4,832
  • 6
  • 40
  • 49
  • Possible duplicate of [Android - Camera preview is sideways](http://stackoverflow.com/questions/3841122/android-camera-preview-is-sideways) – Jeroen Mols Dec 03 '15 at 21:33

1 Answers1

0

You need to tell the OS the orientation of the screen in relationship to the orientation the camera's sensor is mounted in. The orientation of the screen can be rotated, the orientation of the sensor is fixed. You can use the reference code for setDisplayOrientation(int) which can be found here: http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29

RKYoung
  • 43
  • 4