Android bitmap redraw

255 views Asked by At

In my app I take picture from camera and preview it. My app takes picture in landscape mode so that I changed it to portrait mode using exifInterface but I get stretched image. How can I redraw my Image with out stretched image from the bitmap. if I take picture in landscape mode, it looks good.

this is my code for camera part:

public class TakePic extends Fragment {

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        View v = inflater.inflate(R.layout.add_spot_2, container, false);
        ....

        upload.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                String path = Environment.getExternalStorageDirectory()
                        + "/pic";
                File photopath = new File(path);
                if (!photopath.exists()) {
                    photopath.mkdir();
                }

                imagePath = new File(photopath, "pic"
                        + System.currentTimeMillis() + ".png");

                DataPassing.imagePath = imagePath;

                if(imageUri==null){
                    Log.e("Image uri is null", "Image uri is null 1");
                }
                else{
                    Log.e("Image uri is NOT null", "Image uri is NOT null 1");
                }

                Log.e("file path ", imagePath.getAbsolutePath());
                imageUri = Uri.fromFile(imagePath);
                DataPassing.imageUri = imageUri;

                if(imageUri==null){
                    Log.e("Image uri is null ", "Image uri is null 2");
                }
                else{
                    Log.e("Image uri is NOT null", "Image uri is NOT null 2");
                }

                Log.e("uri file path ", imageUri.getPath());

                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(imagePath));
                startActivityForResult(intent, 100);

            }
        });
        ........
        return v;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @SuppressWarnings({ "deprecation" })
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 10;
            if (DataPassing.imageUri == null) {
                Toast.makeText(getActivity(), "Please retry",
                        Toast.LENGTH_SHORT).show();
            } else {
                Bitmap bm = readBitmap(DataPassing.imageUri);

                int or = 0;
                try {
                    or = resolveBitmapOrientation(DataPassing.imagePath);
                    Log.e("int", String.valueOf(or));
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if(or==1){
                    image.setBackgroundDrawable(new BitmapDrawable(bm));
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    byte[] array = stream.toByteArray();
                    AddNewSpotValues.comm_2_picture_path = array;
                    DataPassing.addspot2_bm = bm;
                }
                else{
                    int w = bm.getWidth();
                    int h = bm.getHeight();

                    Log.e("w & h", ""+w  + " & " + h);
                    Matrix mtx = new Matrix();
                    mtx.postRotate(90);
                    Bitmap rbm = Bitmap.createBitmap(bm, 0, 0, w, h, mtx, true);
//                  Bitmap rbm = Bitmap.createBitmap(bm, 0, 0, 200,150, mtx, true);
                    image.setBackgroundDrawable(new BitmapDrawable(rbm));
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    rbm.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    DataPassing.addspot2_bm = rbm;
                    byte[] array = stream.toByteArray();
                    AddNewSpotValues.comm_2_picture_path = array;
                }

            }

        }
    }

    @Override
    public void onLowMemory() {
        // TODO Auto-generated method stub
        super.onLowMemory();
    }

    private Bitmap readBitmap(Uri selectedImage) {
        try {
            File f = new File(selectedImage.getPath());
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 100;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                    && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }

    private int resolveBitmapOrientation(File bitmapFile) throws IOException {
        ExifInterface exif = null;
        exif = new ExifInterface(bitmapFile.getAbsolutePath());

        return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
    }   
}
1

There are 1 answers

1
Ahmad Dwaik 'Warlock' On

You might need to open your camera in portrait mode instead of manipulating the image itself accepted answer here show you how to do it