每当我从图库中获取图像或通过图纸视图中的捕获图像获取图像时,它都会以LANDSCAPE模式旋转

时间:2019-01-28 06:07:08

标签: android canvas

每当我尝试从图库中获取图像或捕获图像并将其带到画布上的工程图视图时,有时图像就会在画布上进入横向模式。我想将其设置为纵向模式。

下面是我的工程图视图代码。

    public class DrawingView extends View {
    Paint mPaint;
    //MaskFilter  mEmboss;
    //MaskFilter  mBlur;
    private int width;
    private  int height;
    Bitmap mBitmap;
    Canvas mCanvas;
    Path mPath;
    Paint   mBitmapPaint;

    public DrawingView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(10);

        mPath = new Path();
        mBitmapPaint = new Paint();
        mBitmapPaint.setColor(Color.RED);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width =w;
        height =h;
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }
    @Override
    public void draw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.draw(canvas);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }
}

我要在布局视图中设置图像,如下所示。

    String s= getFilePathFromURI(ReportNewOneActivity.this, Matisse.obtainResult(data).get(0));
                File file = new File(s);

                if (file.exists()) {
                    Log.e("OnActivityResult ", String.valueOf(file));
                    fp= new File(file.getAbsolutePath());
                    d = Drawable.createFromPath(file.getAbsolutePath());
} else {
                    System.out.println("File Not Found");
                }

1 个答案:

答案 0 :(得分:0)

尝试使用此代码作为解决方案。

String s= getFilePathFromURI(ReportNewOneActivity.this, Matisse.obtainResult(data).get(0));
            File file = new File(s);

            if (file.exists()) {
                Log.e("OnActivityResult ", String.valueOf(file));
                fp= new File(file.getAbsolutePath());
                d = Drawable.createFromPath(file.getAbsolutePath());
                //mDrawingPad.setBackgroundDrawable(d);

                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),bmOptions);

                try {
                    ExifInterface ei = new ExifInterface(file.getAbsolutePath());
                    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_UNDEFINED);


                    switch(orientation) {

                        case ExifInterface.ORIENTATION_ROTATE_90:
                            bitmap = rotateImage(bitmap, 90);
                            BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
                            mDrawingPad.setBackgroundDrawable(ob);
                            break;

                        case ExifInterface.ORIENTATION_ROTATE_180:
                            bitmap = rotateImage(bitmap, 180);
                            BitmapDrawable ob1 = new BitmapDrawable(getResources(), bitmap);
                            mDrawingPad.setBackgroundDrawable(ob1);
                            break;

                        case ExifInterface.ORIENTATION_ROTATE_270:
                            bitmap = rotateImage(bitmap, 270);
                            BitmapDrawable ob2 = new BitmapDrawable(getResources(), bitmap);
                            mDrawingPad.setBackgroundDrawable(ob2);
                            break;

                        case ExifInterface.ORIENTATION_NORMAL:
                        default:
                            bitmap = bitmap;
                            BitmapDrawable obd = new BitmapDrawable(getResources(), bitmap);
                            mDrawingPad.setBackgroundDrawable(obd);

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                    /*img_count = String.valueOf(requestCode);*/
            } else {
                System.out.println("File Not Found");
            }