0

在我的应用程序中,我从相机拍照并预览。我的应用程序以横向模式拍照,因此我使用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);
    }   
}
4

1 回答 1

0

您可能需要以纵向模式打开相机而不是操纵图像本身接受的答案在这里向您展示如何做到这一点

于 2013-11-06T09:26:48.250 回答