单击图片后显示相机图像预览

时间:2015-03-03 09:46:55

标签: android camera whatsapp

我正在制作像什么应用程序的自定义相机。当我打开自定义相机时,它会向我显示与wats app相同的屏幕。

enter image description here

当我点击图片时,我需要在下面的图片中显示预览,就像什么应用程序。我不知道应用程序使用什么机制来显示预览

enter image description here

*注意 - 应用程序是否使用图像视图设置位图以显示图片预览或其他复杂方式。

1 个答案:

答案 0 :(得分:0)

我只是给你一个工作的想法,WhatsApp在后台捕获中做了什么。

答案结果,将布局设计更改为首选,我的设计是基本的。

enter image description here

<强> camera_preview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center_horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:layout_weight="1" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_gravity="center_horizontal"
        android:hint="Add a caption...."
        android:textColorHint="#999999"
        android:background="#333333"
        android:padding="10dp" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:id="@+id/cancel"
            android:layout_weight="1"
            android:textColor="#ffffff" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Okay"
            android:id="@+id/okay"
            android:layout_weight="1"
            android:textColor="#ffffff" />
    </LinearLayout>
</LinearLayout>

class Preview.java

public class Preview extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_preview);
        ImageView image=(ImageView)findViewById(R.id.imageView);

        Intent data=getIntent();

        File imgFile = new File(data.getStringExtra("path"));
        //Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;
        int screenHeight = size.y;

        // Get target image size
        Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        int bitmapHeight = bitmap.getHeight();
        int bitmapWidth = bitmap.getWidth();

        // Scale the image down to fit perfectly into the screen
        // The value must be adjusted for phone/tables displays
        while (bitmapHeight > (screenHeight ) && bitmapWidth > (screenWidth)) {
            bitmapHeight = bitmapHeight / 2;
            bitmapWidth = bitmapWidth / 2;
        }

        // Create resized bitmap image
        BitmapDrawable resizedBitmap = new BitmapDrawable(getResources(),
                Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight,
                        false));


        image.setImageDrawable(resizedBitmap);
    }

    public void onCancelClick(View v){

    }

    public void onOkayClick(View v){

    }

来自您的主要活动startCamera

public void startCamera(){
        File file = new File(path);
        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, 2);
    }

String path声明为全球可靠

内部onCreate方法

path = Environment.getExternalStorageDirectory() + "/example.jpg";
  

我正在开始结果活动而不是开始活动你   可以只显示一个对话框,

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 2 && resultCode == RESULT_OK) {

            Intent startPreview = new Intent(this, Preview.class);
            startPreview.putExtra("path", path);
            startActivity(startPreview);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }