当前位置: 首页 > 知识库问答 >
问题:

覆盖图像方向更改后保存图像从绘图

游高杰
2023-03-14

我是新的Android和工作在一个项目中,我必须使用自定义相机与覆盖图像捕获图像。

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

我已经搜索了很多关于如何改变可绘制图像的方向,但所有方向的改变都是针对位图图像的。若我将可绘制图像更改为位图并更改旋转,它不会显示任何覆盖图像。我不知道代码出了什么问题。我的代码如下。

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

以下是camera imageView和overlay image在mainactivity中的布局调用:

surfaceView=(SurfaceView)findViewById(R.id.cameraView);//用于相机视图surfaceHolder=surfaceView.getHolder(); surfaceHolder.add回调(this); surfaceHolder.set类型(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); ControlInflater=LayoutInflater.from(getBaseContext());//用于在surfaceView视图上膨胀的覆盖视图=ControlInflater.inflate(R.layout.overlay,null);view.setSaveEn的(true); LayoutParams layoutParamsControl=new LayoutParams(LayoutParams...FILL_PARENT,LayoutParams。FILL_PARENT);this.addContentView(view, layoutParamsControl);提前感谢您的帮助。

共有1个答案

荀辰钊
2023-03-14

在对这个主题进行了一些研究之后,我发现旋转(没有动画)或改变图像的方向是不可能的。我浏览了文档位图

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

之后,我添加了以下几行,以便使用onPictureTaken()方法中的setDrawaingCacheEnabled()以与原始图像相同的方向从drawable访问图像。

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

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

可绘制。setBounds(0,0,drawable.getIntrinsicWidth()*1/2,drawable.getIntrinsicHeight()*1/2)

 类似资料:
  • 保存图像 能将图像保存至Memory Stick™或主机内存。 1. 让指针对准想要保存的图像,从选单列中选择[档案] > [保存图像]。 2. 选择[保存]。 提示 若想变更文件名或保存位置,请选择各项输入栏,并执行决定。

  • 问题内容: 我需要将图像从PHP URL保存到PC。假设我有一个页面,其中仅包含一个“花”图像,仅此而已。如何使用新名称(使用PHP)从URL中保存该图像? 问题答案: 如果您设置为: 其他使用cURL:

  • 问题内容: 我在使用python通过urllib2请求或urllib.urlretrieve从url保存图像时遇到问题。那是图像的URL是有效的。我可以使用资源管理器手动下载它。但是,当我使用python下载图像时,无法打开该文件。我使用Mac OS预览来查看图像。谢谢! 更新: 代码如下 我要下载的图像URL是 http://site.meishij.net/r/58/25/3568808/a3

  • 基本上,我有一个方法可以将Image从数据库加载到ImageView中,还有第二个方法可以更改图像,我成功地运行了这两个方法,没有出现异常,但是在change eImage()方法中的setImage之后,我需要更新什么以及如何(场景、阶段)是可能的。我知道在javafx中没有像swings中的repaint()这样的方法,那么我该如何处理呢?

  • 我刚刚在文件夹下添加了一个新的可绘制文件夹。在drawable文件夹中,我复制了ic\U启动器。png文件来自可绘制hdpi文件夹。当我按下按钮时,我想通过新的图像更改标准的图像按钮。我写了一些代码,但当我启动应用程序时,它崩溃了。 编辑:我改成了这个,这个也不行。 编辑2:这很有效。感谢大家。