Перерисовка растрового изображения Android

В моем приложении я делаю снимок с камеры и просматриваю его. Мое приложение делает снимок в ландшафтном режиме, поэтому я изменил его на портретный режим с помощью exifInterface, но я получаю растянутое изображение. Как я могу перерисовать свое изображение без растянутого изображения из растрового изображения. если я снимаю в ландшафтном режиме, это выглядит хорошо.

это мой код для части камеры:

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);
    }   
}

person M.A.Murali    schedule 06.11.2013    source источник
comment
где вы инициализируете image? можешь опубликовать xml   -  person Amulya Khare    schedule 06.11.2013
comment
можете опубликовать макет .. где определяется изображение? я думаю, вы, возможно, упустили какое-то свойство просмотра изображения   -  person Amulya Khare    schedule 06.11.2013


Ответы (1)


Возможно, вам придется открыть камеру в портретном режиме вместо того, чтобы манипулировать самим изображением. -mode-in-android">здесь показать вам, как это сделать

person Ahmad Dwaik 'Warlock'    schedule 06.11.2013
comment
Я не использовал поверхность. Я использую изображение только для предварительного просмотра. Как изменить этот код? - person M.A.Murali; 06.11.2013