簡體   English   中英

如何獲取位圖到字節數組android?

[英]How to get bitmap to byte array android?

我瀏覽了其他一些帖子,但似乎找不到答案。 當我運行該應用程序並拍攝第一張照片時,用於存儲它的全類數組將獲得位圖的正確值。 但是,當我拍攝第二張照片時,它只會覆蓋第一張照片,而將第二張照片留空。 我猜想這與重寫的方法有關,但是我不確定。

這是我的代碼:

package vikin.example.com.picturecomparisonv2;

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

import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
public byte[] firstImage = null;
public byte[] secondImage = null;
long hits;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageView = findViewById(R.id.imageView);
}

public void takePicture(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent,1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        try {
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imageView.setImageBitmap(imageBitmap);
            setByteArrays(imageBitmap);
        } catch(NullPointerException e) {
            e.printStackTrace();
        }
    }
}





public void analyze(View view) {
    if(firstImage != null && secondImage != null) {
        int length = 0;
        if(firstImage.length <= secondImage.length) {
            length = firstImage.length;
        } else if(firstImage.length >= secondImage.length) {
            length = secondImage.length;
        }
        for(int i = 0; i < length; i++) {
            if(firstImage[i] == secondImage[i]) {
                hits++;
            }
        }
        if(hits == length) {
            Toast.makeText(this, "Images are exaclty identical",Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Images are different", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(this, "Need 1 or more images", Toast.LENGTH_LONG).show();
    }
}

public void setByteArrays(Bitmap bitmap) {
    try {
        if(firstImage == null) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] firstImageArray = stream.toByteArray();
            firstImage = firstImageArray;
        } else if(secondImage == null) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] secondImageArray = stream.toByteArray();
            secondImage = secondImageArray;
        } else {
             Toast.makeText(this, "Only two pictures allowed", Toast.LENGTH_LONG).show();
        }
     } catch(NullPointerException e) {
     e.printStackTrace();
 }
}
}

任何幫助深表感謝。

可以這樣寫函數

public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
public byte[] firstImage = null;
public byte[] secondImage = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
long hits;

public void setByteArrays(Bitmap bitmap) {
    try {
        if(firstImage == null) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            firstImage = stream.toByteArray();
        } else if(secondImage == null) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            secondImage = stream.toByteArray();
        } else {
             Toast.makeText(this, "Only two pictures allowed", Toast.LENGTH_LONG).show();
        }
     } catch(NullPointerException e) {
     e.printStackTrace();
 }
}
}

因為您只替換通過值傳遞獲得的引用,所以您不會在調用方方法中更改引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM