android - 如何在 Android SurfaceView 上制作裁剪相机 View

标签 android image image-processing imageview surfaceview

我想在 Android 操作系统上使用 SurfaceView 创建一个裁剪应用程序。我制作了一个用于显示直视相机的显示表面,但是当我想直接在相机显示屏上添加裁剪功能时,我仍然失败。下面的图编辑将完成:

enter image description here

当点击拍摄按钮时,图像结果将出现在屏幕上,如下所示:

enter image description here

以下是MainActivity.java源码

package com.example.CameraPreview1;

import java.io.IOException;
import java.util.List;

import curso.citic15.camara.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity implements SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback {
    private SurfaceView Surface;
    Camera camera;
    Bitmap InputImages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Surface = (SurfaceView) findViewById(R.id.surface);
        Surface.getHolder().addCallback(this);

        ImageButton shutter = (ImageButton) findViewById(R.id.button_capture);
        shutter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                camera.takePicture(MainActivity.this, null, null, MainActivity.this);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        camera.release();
    }

    @Override
    protected void onPause() {
        super.onPause();
        camera.stopPreview();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onShutter() {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (camera != null) {
            Camera.Parameters params = camera.getParameters();
            List<Camera.Size> sizes = params.getSupportedPreviewSizes();
            Camera.Size selected = sizes.get(0);
            params.setPreviewSize(selected.width, selected.height);
            camera.setParameters(params);
            camera.setDisplayOrientation(90);


            try {
                camera.setPreviewDisplay(holder);
            } catch (IOException e) {
                e.printStackTrace();
            }

            camera.startPreview();
        } else {
            Toast.makeText(this, "No hay camara o hay algœn error.", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d("", "Destroyed");
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 6;

        InputImages = BitmapFactory.decodeByteArray(data, 0, data.length, options);

        camera.startPreview();
    }

}

下面是源代码main_activity.xml

<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=".MainActivity" >

<SurfaceView
    android:id="@+id/surface"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<ImageButton
    android:id="@+id/button_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="15dp"
    android:layout_marginRight="20dp"
    android:src="@drawable/dua"
    android:background="@null"
     />

<ImageButton
    android:id="@+id/button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="15dp"
    android:layout_marginLeft="20dp"
    android:src="@drawable/satu"
    android:background="@null"
     />

<ImageButton
    android:id="@+id/button_capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="10dp"
    android:src="@drawable/capture"
    android:background="@null"
     />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@null" />
</RelativeLayout>

我一直在 Google 上查找,但没有一个教程提供我想要的东西,我有一点压力和绝望。能帮忙解决一下上面提到的问题吗???如果有帮助的话我会很高兴。非常感谢

最佳答案

查看本教程。显示相机预览和自定义 View ,可以通过拖动来定义要裁剪的区域。您可以根据您的需要进行修改。我发现的唯一问题是屏幕方向、修改预览以及生成的图像的方向。

http://adblogcat.com/a-camera-preview-with-a-bounding-box-like-google-goggles/

关于android - 如何在 Android SurfaceView 上制作裁剪相机 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779151/

相关文章:

css - 使用 css 将鼠标悬停在按钮上时如何更改网站背景图片?

python - 在图像中找到矩形并提取其中的文本以将其另存为新图像

android - Abt 检索设备的纬度和经度

android - 通过构造函数将参数传递给 SurfaceView

javascript - Ionic - 后退按钮关闭键盘但输入仍然聚焦

image-processing - 计算机视觉/道路跟踪入门

Java将原始字节编码为图像简单图像格式/文件

android - 高亮显示的 EditText 文本颜色

android - picasso - 如何获得真实的图像尺寸?

HTML/CSS 图像定位问题