Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change save image Orientation in Portitate mode in android using Surfaceholder

Tags:

android

public class MainActivity extends Activity {

    ImageView image;
    Activity context;
    Preview preview;
    static Camera camera;
    Button exitButton;
    ImageView fotoButton;
    LinearLayout progressLayout;
    String path = "/sdcard/KutCamera/cache/images/";
    public static int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = this;

        fotoButton = (ImageView) findViewById(R.id.imageView_foto);
        exitButton = (Button) findViewById(R.id.button_exit);
        image = (ImageView) findViewById(R.id.imageView_photo);
        progressLayout = (LinearLayout) findViewById(R.id.progress_layout);

        preview = new Preview(this,
                (SurfaceView) findViewById(R.id.KutCameraFragment));
        FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
        frame.addView(preview);
        preview.setKeepScreenOn(true);
        fotoButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    takeFocusedPicture();
                } catch (Exception e) {

                }
                exitButton.setClickable(false);
                fotoButton.setClickable(false);
                progressLayout.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // TODO Auto-generated method stub
        if (camera == null) {
            camera = Camera.open();
            camera.startPreview();
            camera.setErrorCallback(new ErrorCallback() {
                public void onError(int error, Camera mcamera) {

                    camera.release();
                    camera = Camera.open();
                    Log.d("Camera died", "error camera");

                }
            });
        }
        if (camera != null) {
            if (Build.VERSION.SDK_INT >= 14)
                setCameraDisplayOrientation(context,
                        CameraInfo.CAMERA_FACING_BACK, camera);
            preview.setCamera(camera);
        }
    }

    public static void setCameraDisplayOrientation(Activity activity,
            int cameraId, android.hardware.Camera camera) {
        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        int rotation = activity.getWindowManager().getDefaultDisplay()
                .getRotation();
        int degrees = 0;
        switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
        }

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360; // compensate the mirror
        } else { // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }
        camera.setDisplayOrientation(result);
    }

    Camera.AutoFocusCallback mAutoFocusCallback = new Camera.AutoFocusCallback() {
        @Override
        public void onAutoFocus(boolean success, Camera camera) {

            try {

                camera.takePicture(mShutterCallback, null, jpegCallback);


            } catch (Exception e) {

            }

        }
    };

    Camera.ShutterCallback mShutterCallback = new ShutterCallback() {

        @Override
        public void onShutter() {
            // TODO Auto-generated method stub

        }
    };

    public void takeFocusedPicture() {
        camera.autoFocus(mAutoFocusCallback);

    }

    PictureCallback jpegCallback = new PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {

            FileOutputStream outStream = null;
            Calendar c = Calendar.getInstance();
            File videoDirectory = new File(path);

            if (!videoDirectory.exists()) {
                videoDirectory.mkdirs();
            }

            try {
                // Write to SD Card
                outStream = new FileOutputStream(path
                        + c.getTime().getSeconds() + ".jpg");
                outStream.write(data);

                Toast.makeText(MainActivity.this, "Image:" + count, 1000)
                        .show();
                count++;

                outStream.close();
                if (count == 3) {
                    finish();
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {

            }

            Bitmap realImage;
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 5;
            options.inPurgeable = true; 
            options.inInputShareable = true;
            realImage = BitmapFactory.decodeByteArray(data, 0, data.length,
                    options);

            if (data != null) {

                int screenWidth = getResources().getDisplayMetrics().widthPixels;
                int screenHeight = getResources().getDisplayMetrics().heightPixels;

                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    // Notice that width and height are reversed
                    Bitmap scaled = Bitmap.createScaledBitmap(realImage,
                            screenHeight, screenWidth, true);
                    int w = scaled.getWidth();
                    int h = scaled.getHeight();
                    // Setting post rotate to 90
                    Matrix mtx = new Matrix();
                    mtx.postRotate(90);
                    // Rotating Bitmap
                    realImage = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx,
                            true);
                } else {

                    Bitmap scaled = Bitmap.createScaledBitmap(realImage,
                            screenWidth, screenHeight, true);
                    realImage = scaled;
                }

            }

            image.setImageBitmap(realImage);

            fotoButton.setClickable(true);
            camera.startPreview();
            progressLayout.setVisibility(View.GONE);
            exitButton.setClickable(true);

        }
    };

}

This is my code using this i am taking Picture from camera and save in Sdcard in given path i am taking image from camera in portrait mode its working fine and save in Sd card But saved Image when i open its Landscape Mode but i want keep it in portrait mode but i am trying change many thing in surface holder class and even Main actvity class not chaining while if set camera preview in land scape then its saved Image saving in portrait mode while i want camera preview and saved image in Sd card both should in portrait mode please suggest me how to do this

like image 685
Research Development Avatar asked Sep 04 '15 04:09

Research Development


People also ask

How do I change the orientation of a picture on android?

Open your device's Settings app. . Select Accessibility. Select Auto-rotate screen.

How do I manage screen orientation on Android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What happens on orientation change android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them . Android does this so that your application can reload resources based on the new configuration.


1 Answers

same issue i was facing but using this code its work for me try this code:

Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                Matrix matrix = new Matrix();
                matrix.postRotate(90);    
                Bitmap bitmapFinal=null;

                bitmapFinal = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                bmp.recycle();
                bmp=null;

                FileOutputStream fos = new FileOutputStream(pictureFile);
                if(imageFormat.equals("jpg")||imageFormat.equals("jpeg"))
                {
                    bitmapFinal.compress(Bitmap.CompressFormat.JPEG, 100,fos);
                }
                else if(imageFormat.equals("png"))
                {
                    bitmapFinal.compress(Bitmap.CompressFormat.PNG, 100,fos);
                }
                else 
                {
                    bitmapFinal.compress(Bitmap.CompressFormat.JPEG, 100,fos);
                }

                bitmapFinal.recycle();
                bitmapFinal=null;

                fos.flush();
                fos.close();
                fos=null;
like image 181
Edge Avatar answered Oct 27 '22 01:10

Edge