-1

我把相機加載到一個framelayout中,我想截圖。的相機預覽。如何在Android中拍攝自定義相機的截圖?

採取截圖:

public void onClick(View v) { 
    View v1 = L1.getRootView(); 
    v1.setDrawingCacheEnabled(true); 
    Bitmap bm = v1.getDrawingCache(); 
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm); 
    image = (ImageView) findViewById(R.id.ImageView011); 
    image.setBackgroundDrawable(bitmapDrawable); 

    //Bitmap bitmap = takeScreenshot(); 
    saveBitmap(bm); 
} 

保存截圖:

public void saveBitmap(Bitmap bitmap) { 
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); 
    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(imagePath); 
     bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, fos); 
     fos.flush(); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     Log.e("GREC", e.getMessage(), e); 
    } catch (IOException e) { 
     Log.e("GREC", e.getMessage(), e); 
    } 
} 

當圖像被保存一切都在截圖,除了相機預覽 enter image description here

相機框架佈局內的負載:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="mobapptut.com.camapp.bellowLollipop" 
android:baselineAligned="false" 
android:id="@+id/containerImg"> 


<FrameLayout 
android:id="@+id/camera_preview" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_alignParentLeft="true" 
android:layout_alignParentStart="false"> 


</FrameLayout> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView android:id="@+id/ImageView01" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" /> 

</LinearLayout> 
<ImageView 
android:layout_width="match_parent" 
android:layout_height="300dp" 
android:id="@+id/captured_image" 
android:layout_alignParentLeft="true" 
android:layout_alignParentStart="true" 
android:contentDescription="desc" /> 


<ImageButton 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/plusBtnImg" 
android:layout_gravity="left|bottom" 
android:src="@mipmap/zoom_in2" 
android:layout_alignParentBottom="true" 
android:layout_toEndOf="@+id/imageView2" 
android:background="#00ffffff" /> 


<ImageButton 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/minusBtnImg" 
android:layout_gravity="left|bottom" 
android:src="@mipmap/zoom_out2" 
android:layout_above="@+id/plusBtnImg" 
android:layout_toRightOf="@+id/imageView2" 
android:background="#00ffffff" /> 

<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Go Back!" 
android:id="@+id/backButton" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" 
android:layout_gravity="right|bottom" 
android:layout_alignParentRight="true" /> 

<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Save Picture" 
android:id="@+id/button12" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true"/> 

<Button 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Take Screenshot" 
android:id="@+id/Button01" 
/> 

<ImageView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Take Screenshot" 
android:id="@+id/ImageView011" 
/> 
</RelativeLayout> 

我在做什麼錯誤?感謝提前:)

+0

您是否檢查[this](http://stackoverflow.com/questions/13565221/taking-screenshot-camera-viewlayout-view-android-augmented-reality)? – solosodium

回答

3

只是通過你需要的視圖的參數在下面的方法

public Bitmap takeScreenShot(View view) { 
    view.setDrawingCacheEnabled(true); 
    view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); 
    view.buildDrawingCache(); 
    if (view.getDrawingCache() == null) 
     return null; 
    Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache()); 
    view.setDrawingCacheEnabled(false); 
    view.destroyDrawingCache(); 
    return snapshot; 
} 

而這個位圖保存,按您的需求。

相關問題