【问题标题】:Overlay Image orientation changed after saving Image from drawable从drawable保存图像后覆盖图像方向更改
【发布时间】:2017-01-12 07:30:32
【问题描述】:

我是 Android 新手,正在从事一个项目,我必须使用带有叠加图像的自定义相机来捕捉图像。

我可以使用覆盖(组合图像)捕获图像,但问题是在保存新的组合图像后覆盖图像方向发生变化,即覆盖图像主要设置为纵向模式,但保存后显示为横向模式.

我已经搜索了很多 如何更改 Drawable Image 的方向,但任何地方的方向更改都是针对位图图像。如果我将可绘制图像更改为位图并更改旋转它不会显示任何叠加图像.我不知道代码有什么问题。 我的代码如下。

public void onPictureTaken(byte[] arg0, Camera camera) {
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
        int   wid = cameraBitmap.getWidth();
        int  hgt = cameraBitmap.getHeight();
        Bitmap newImage = Bitmap.createBitmap(wid,hgt,Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newImage);
        canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
      // Drawable drawable = getResources().getDrawable(R.drawable.frame);
       // drawable.setBounds(0,0,drawable.getIntrinsicWidth()*1/2, drawable.getIntrinsicHeight()*1/2);
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.frame);
        Matrix matrix = new Matrix();
        matrix.postRotate(270);
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, 100, 200, true);
        Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
       Drawable drawable = new BitmapDrawable(getResources(), rotatedBitmap);
        drawable.draw(canvas);
        File storagePath = new File(Environment.getExternalStorageDirectory() + "/Photo/");
        storagePath.mkdirs();
        File myImage = new File(storagePath,Long.toString(System.currentTimeMillis()) + ".jpg");
        try
        { 
          FileOutputStream out = new FileOutputStream(myImage);
            ExifInterface exif = new ExifInterface(cameraBitmap.toString());
            Log.d("EXIF value", exif.getAttribute(ExifInterface.TAG_ORIENTATION));
            if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")) {
                newImage = rotate(newImage, 90);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")) {
                newImage = rotate(newImage, 270);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")) {
                newImage = rotate(newImage, 180);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")) {
                newImage = rotate(newImage, 90);}
            newImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
            camera.startPreview();
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse("file://" + myImage.getAbsolutePath()), "image/*");
            startActivity(intent);
            newImage.recycle();
            newImage = null;
            out.flush();
            out.close();
        }
        catch(FileNotFoundException e)
        {
            Log.d("In Saving File", e + "");
        }
        catch(IOException e)
        {
            Log.d("In Saving File", e + "");
        }
    }};

      public Bitmap rotate(Bitmap bitmap, int degree) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Matrix mtx = new Matrix();
    //       mtx.postRotate(degree);
    mtx.setRotate(degree);
    return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}`

以下是在 mainactivity 中对相机 imageView 和叠加图像的布局调用:

surfaceView = (SurfaceView) findViewById(R.id.cameraView);//for camera view surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); controlInflater = LayoutInflater.from(getBaseContext());//for overlay view inflated over the surfaceView View view = controlInflater.inflate(R.layout.overlay, null); view.setSaveEnabled(true); LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); this.addContentView(view, layoutParamsControl); 提前感谢您的帮助。

【问题讨论】:

    标签: android image


    【解决方案1】:

    在对该主题进行一些研究后,我发现无法旋转(没有动画)或更改可绘制图像的图像方向。 我浏览了文档BitmapView。 View 文档指出:

    void setDrawingCacheEnabled(启用布尔值) 启用或禁用图形缓存。启用绘图缓存后,下一次调用 getDrawingCache() 或 buildDrawingCache() 将在位图中绘制视图。启用缓存时,调用 draw(android.graphics.Canvas) 不会从缓存中绘制。

    之后,我添加以下几行,以便通过在 onPictureTaken() 方法中使用 setDrawaingCacheEnabled() 以与原始相同的方向从可绘制对象访问图像。

     view.setDrawingCacheEnabled(true);
            Bitmap viewCapture = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);
    

    除了在输出屏幕中设置可绘制图像尺寸之外,还需要使用 setBounds 方法(我在我的问题中忽略了该方法)

    drawable.setBounds(0, 0, drawable.getIntrinsicWidth() * 1/2, drawable.getIntrinsicHeight() * 1/2);

    【讨论】:

      猜你喜欢
      • 2019-11-10
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 2013-11-25
      • 2012-05-25
      • 2020-03-25
      • 1970-01-01
      • 2016-02-13
      相关资源
      最近更新 更多