簡體   English   中英

拍攝屏幕快照相機視圖+布局視圖android增強現實

[英]taking screenshot camera view+layout view android augmented reality

我嘗試從幀緩沖區中獲取屏幕捕獲,它非常適合我的布局視圖,我在這里使用了以下代碼:

String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

但是我想用相機視圖捕獲布局視圖,因為我也在布局視圖或透明網頁的背景中使用cameraview。 一種解決方法是分別拍攝相機圖像和布局視圖,並將它們彼此放置並保存新的位圖。

有人可以建議我對此采取任何適當的解決方案嗎? 提前致謝

為了將兩個圖像放在一起,您可能希望遵循此代碼以其100%的效果將兩個圖像疊加在一起

 Bitmap border = BitmapFactory.decodeResource(getResources(), R.drawable.vignette2);
int width = bmp.getWidth();
int height = bmp.getHeight();
change = Bitmap.createScaledBitmap(change, width, height, false); // change is Bitmap
Canvas canvas = new Canvas(change);
Bitmap scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0,null);
//canvas.drawBitmap(k, 0, 0, null);
view.setImageBitmap(change); // view is the imageView

現在是保存視圖的一部分,簡單的方法是

view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        save = view.getDrawingCache(); // save is a Bitmap

接着 :

                final FileOutputStream out = new FileOutputStream(file);
            save.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));
            Toast.makeText(getApplication(), "Image Saved",
                    Toast.LENGTH_SHORT).show();

這將為您保存imageview的視圖,或者如果它的Layout只是設置您的布局並給它命名,然后用view替換它,請在您檢查保存是否成功設置后不要忘記

        view.setDrawingCacheEnabled(false); 

我無法將代碼發布為注釋,因此您可以在onActivtity上傳遞該特定方法,或者在您發送意圖或自定義攝像頭后傳遞它,然后可以在單擊“攝像頭”按鈕時進行控制只需添加一種方法即可捕獲視圖。 這是我保存視圖的方法。

void Save() {
    if (null != view.getDrawable()) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        save = view.getDrawingCache();
        final File myDir = new File(folder);
        myDir.mkdirs();
        final Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        final String fname = "StyleMe-" + n + ".png";
        final File file = new File(myDir, fname);
        if (file.exists())
            file.delete();
        try {
            final FileOutputStream out = new FileOutputStream(file);
            save.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));
            Toast.makeText(getApplication(), "Image Saved",
                    Toast.LENGTH_SHORT).show();
        } catch (final Exception e) {
            Toast.makeText(getApplication(),
                    "Something Went Wrong check if you have Enough Memory",
                    Toast.LENGTH_LONG).show();
        }
    } else {
        final Toast tst = Toast.makeText(getApplication(),
                "Please Select An Image First", Toast.LENGTH_LONG);
        tst.setGravity(Gravity.CENTER, 0, 0);
        tst.show();
    }
    view.setDrawingCacheEnabled(false);
}

為了讓您連續保存視圖,只需添加loop即可。 當您單擊相機按鈕時,循環開始! 在這里,在我的保存方法中,不必擔心重疊名稱:)。 它將隨機生成每個圖像的名稱!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM