gpt4 book ai didi

java - 拍照后如何在另一个 Activity 中显示图像?

转载 作者:行者123 更新时间:2023-12-02 02:39:51 25 4
gpt4 key购买 nike

我想要我在另一项 Activity 中拍摄并展示的照片。我最好希望有一个用于下一个 Activity 的按钮,单击时会显示我拍摄的图像。只需要一个简单的方法。我是 android studio 的新手,因此我不太确定如何解决这个问题。

这是我的代码

扫描仪.java

package mapp.com.sg.receiptscanner;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;



public class Scanner extends AppCompatActivity {

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

Button btnCamera = (Button)findViewById(R.id.btnCamera);
imageView = (ImageView)findViewById(R.id.imageView);

btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);


}

public void onClick(View view) {
Intent i = new Intent(this, Info.class);
startActivity(i);
}
}

activity_scanner.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="mapp.com.sg.receiptscanner.Scanner">

<ImageView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="9"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#1f4991"
android:textColor="@android:color/white"
android:text="Open Camera"
android:id="@+id/btnCamera"
android:layout_marginBottom="14dp"
android:layout_above="@+id/ProceedBtn"
android:layout_alignStart="@+id/ProceedBtn" />

<Button
android:id="@+id/ProceedBtn"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Proceed"
android:background="#1f4991"
android:textColor="@android:color/white"
android:onClick="onClick"/>

最佳答案

尝试将您的照片转换为字节并将其发送到另一个 Activity 。

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();

Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("photo", image);
startActivity(intent);

并在下一个 Activity 中获取它并使用decodeByteArray创建位图并设置到 ImageView 中。

YourActivity.class

byte[] byteArrayExtra = getIntent().getByteArrayExtra("photo");

//BitmapOptions is optional you can create bitmap without this also. This is the description of its use from google developer docs.
//BitmapFactory.Options: null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned.

Bitmap bitmap = BitmapFactory.decodeByteArray(byteArrayExtra, 0, byteArrayExtra.length, new BitmapFactory.Options());

//or
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArrayExtra, 0, byteArrayExtra.length);


imageview.setimageBitmap(bitmap);

关于java - 拍照后如何在另一个 Activity 中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45678008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com