2013-02-15 33 views
6

Làm cách nào để thêm lớp phủ hình chữ nhật (lớp phủ hình chữ nhật giống như khung hình chữ nhật) trong chế độ xem trước của máy ảnh? Ứng dụng của tôi bao gồm một nút mở máy ảnh khi nhấp vào. Tôi cần lớp phủ trong bản xem trước máy ảnh đó.Cách thêm lớp phủ hình chữ nhật trong ứng dụng máy ảnh?

Mã cho file java là:

public class MainActivity extends Activity { 
    Button b; 
    Intent i; 
    Bitmap bmp; 
    final static int cameraData = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     b = (Button) findViewById(R.id.button); 
     InputStream is = getResources().openRawResource(R.drawable.ic_launcher); 
     bmp = BitmapFactory.decodeStream(is); 
     b.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
      // TODO Auto-generated method stub 
      i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(i, cameraData); 

     } 
    }); 
} 
} 

File Layout như sau:

<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" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_gravity="center" 
    android:text="click to take photo" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="click" 
    android:layout_gravity="center" /> 

</LinearLayout> 

Trả lời

4

Bạn phải tạo Preview của riêng bạn bằng cách mở rộng các lớp SurfaceView.

Xem liên kết dưới đây sẽ giúp bạn.

Custom camera android

Hãy FrameLayout với SurfaceView như child.and tùy chỉnh theo nhu cầu của bạn

+0

Làm cách nào để vẽ hình chữ nhật? –

+0

bạn có thể chụp một số hình ảnh và đặt nó lên trên đầu trang của surfaceview – koti

+0

ok .. Vì vậy, bạn đề nghị tôi đặt hình ảnh của một hình chữ nhật? Nó có minh bạch không? –

13

Đầu tiên tạo ra một lớp công cộng kéo dài View. Bên trong phương thức onDraw() của nó, vẽ hình chữ nhật của bạn. Ví dụ:

public class Box extends View { 
    private Paint paint = new Paint(); 
    Box(Context context) { 
    super(context); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { // Override the onDraw() Method 
    super.onDraw(canvas); 

    paint.setStyle(Paint.Style.STROKE); 
    paint.setColor(Color.GREEN); 
    paint.setStrokeWidth(10); 

    //center 
    int x0 = canvas.getWidth()/2; 
    int y0 = canvas.getHeight()/2; 
    int dx = canvas.getHeight()/3; 
    int dy = canvas.getHeight()/3; 
    //draw guide box 
    canvas.drawRect(x0-dx, y0-dy, x0+dx, y0+dy, paint); 
    } 
} 

Sau đó, trong Hoạt động xem trước máy ảnh của bạn, có một thể hiện lớp Box

Box box = new Box(this); 

Sau đó,

addContentView(box, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

Một hình chữ nhật màu xanh lá cây sẽ được tô lên máy ảnh của bạn xem trước. Chúc may mắn! Đây là một liên kết đã giúp tôi Draw on camera preview

+0

Bạn chỉ là thiên tài! Cảm ơn bạn rất nhiều và Chúa ban phước !!! – Vincy

+0

Đây phải là câu trả lời đúng – graell