Android 如何将字节数组显示为图像

Android 如何将字节数组显示为图像,android,bitmap,bytearray,converter,pixel,Android,Bitmap,Bytearray,Converter,Pixel,我将图像转换为灰度,然后像这样读取像素 for (int i = 0; i < image.getHeight(); i++) { for (int j = 0; j < image.getWidth(); j++) { pixels[k]=(byte) (image.getPixel(j, i)); pix

我将图像转换为灰度,然后像这样读取像素

 for (int i = 0; i < image.getHeight(); i++)
            {
                for (int j = 0; j < image.getWidth(); j++)
                {
                      pixels[k]=(byte) (image.getPixel(j, i));
                      pixels2[k]=unsignedToBytes(pixels[k]);
                      k++;
                }
            }
请帮助我们……

public int edgedetection6(int[][] oimage,int tblock) 
{
    ImageView imageView1 = (ImageView) findViewById(R.id.imgView1);
    int edgeImage[][]=new int[42][42];
    byte edgeImage1[]=new byte[42*42];
    int n=0;
    //int s[][]={{-1,-1,-1},{-1,8,-1},{-1,-1,-1}};
    long R=0;
    for(int i=0;i<42;i++)
    {
        for(int j=0;j<42;j++)
        {
            if(i==0 || j==0 || i==41 || j==41 )
            edgeImage[i][j]=0;
            else
                edgeImage[i][j]=oimage[i-1][j-1];

        }
    }
    for(int i=0;i<40;i++)
    {
        for(int j=0;j<40;j++)
        {
            R=8*edgeImage[i+1][j+1]-(edgeImage[i][j]+edgeImage[i][j+1]+edgeImage[i][j+2]+edgeImage[i+1][j]+edgeImage[i+1][j+2]+edgeImage[i+2][j]+edgeImage[i+2][j+1]+edgeImage[i+2][j+2]);
            if(R>0)
                edgeImage[i+1][j+1]=1;
            else
                edgeImage[i+1][j+1]=0;
            edgeImage1[n]=(byte)edgeImage[i][j];
            n++;
        }

    }

               ImageView imageView1 = (ImageView) findViewById(R.id.imgView1);
           imageView1.setImageBitmap(BitmapFactory.decodeByteArray(edgeImage1, 0, edgeImage.length));
public int-edgedetection6(int[]oimage,int-tblock)
{
ImageView imageView1=(ImageView)findViewById(R.id.imgView1);
int edgeImage[][]=新int[42][42];
字节edgeImage1[]=新字节[42*42];
int n=0;
//int s[][]={{-1,-1,-1},{-1,8,-1},{-1,-1};
长R=0;

对于(inti=0;i为类帧模式传递额外的行和列是可以的

但我认为你发布的边缘检测代码是危险的

让我给你一个边缘检测的样本,供你参考

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class ImageProcessingActivity extends Activity {

 final static int KERNAL_WIDTH = 3;
 final static int KERNAL_HEIGHT = 3;

 int[][] kernal ={
   {0, -1, 0},
   {-1, 4, -1},
   {0, -1, 0}
 };

 ImageView imageSource, imageAfter;
 Bitmap bitmap_Source;
 ProgressBar progressBar;

 private Handler handler;
 Bitmap afterProcess;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageSource = (ImageView)findViewById(R.id.imageSource);
        imageAfter = (ImageView)findViewById(R.id.imageAfter);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);

        bitmap_Source = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        handler = new Handler();
        StratBackgroundProcess();
    }

    private void StratBackgroundProcess(){

     Runnable runnable = new Runnable(){

   @Override
   public void run() {
    afterProcess = processingBitmap(bitmap_Source, kernal);

    handler.post(new Runnable(){

     @Override
     public void run() {
      progressBar.setVisibility(View.GONE);
      imageAfter.setImageBitmap(afterProcess);
     }

    });
   }
     };
     new Thread(runnable).start();
    }

    private Bitmap processingBitmap(Bitmap src, int[][] knl){
     Bitmap dest = Bitmap.createBitmap(
       src.getWidth(), src.getHeight(), src.getConfig());

     int bmWidth = src.getWidth();
     int bmHeight = src.getHeight();
     int bmWidth_MINUS_2 = bmWidth - 2;
     int bmHeight_MINUS_2 = bmHeight - 2;

     for(int i = 1; i <= bmWidth_MINUS_2; i++){
      for(int j = 1; j <= bmHeight_MINUS_2; j++){

       //get the surround 3*3 pixel of current src[i][j] into a matrix subSrc[][]
       int[][] subSrc = new int[KERNAL_WIDTH][KERNAL_HEIGHT];
       for(int k = 0; k < KERNAL_WIDTH; k++){
        for(int l = 0; l < KERNAL_HEIGHT; l++){
         subSrc[k][l] = src.getPixel(i-1+k, j-1+l);
        }
       }

       //subSum = subSrc[][] * knl[][]
       int subSumA = 0;
       int subSumR = 0;
       int subSumG = 0;
       int subSumB = 0;

       for(int k = 0; k < KERNAL_WIDTH; k++){
        for(int l = 0; l < KERNAL_HEIGHT; l++){
         subSumR += Color.red(subSrc[k][l]) * knl[k][l];
         subSumG += Color.green(subSrc[k][l]) * knl[k][l];
         subSumB += Color.blue(subSrc[k][l]) * knl[k][l];
        }
       }

       subSumA = Color.alpha(src.getPixel(i, j));

       if(subSumR <0){
        subSumR = 0;
       }else if(subSumR > 255){
        subSumR = 255;
       }

       if(subSumG <0){
        subSumG = 0;
       }else if(subSumG > 255){
        subSumG = 255;
       }

       if(subSumB <0){
        subSumB = 0;
       }else if(subSumB > 255){
        subSumB = 255;
       }

       dest.setPixel(i, j, Color.argb(
         subSumA,
         subSumR,
         subSumG,
         subSumB));
      }
     }

     return dest;
    }

}
导入android.app.Activity;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.os.Handler;
导入android.view.view;
导入android.widget.ImageView;
导入android.widget.ProgressBar;
公共类ImageProcessingActivity扩展活动{
最终静态int核_宽度=3;
最终静态整数内核高度=3;
int[][]内核={
{0, -1, 0},
{-1, 4, -1},
{0, -1, 0}
};
ImageView imageSource,imageAfter;
位图源;
ProgressBar ProgressBar;
私人经办人;
位图后处理;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSource=(ImageView)findViewById(R.id.imageSource);
imageAfter=(ImageView)findViewById(R.id.imageAfter);
progressBar=(progressBar)findViewById(R.id.progressBar);
bitmap\u Source=BitmapFactory.decodeResource(getResources(),R.drawable.ic\u启动器);
handler=新的handler();
StratBackgroundProcess();
}
私人背景流程(){
Runnable Runnable=新的Runnable(){
@凌驾
公开募捐{
后处理=处理位图(位图\源,内核);
handler.post(新的Runnable(){
@凌驾
公开募捐{
progressBar.setVisibility(View.GONE);
设置图像位图(后处理);
}
});
}
};
新线程(runnable.start();
}
专用位图处理位图(位图src,int[][]knl){
位图dest=Bitmap.createBitmap(
src.getWidth()、src.getHeight()、src.getConfig());
int bmWidth=src.getWidth();
int bmHeight=src.getHeight();
int bmWidth_减去_2=bmWidth-2;
int bmHeight_减2=bmHeight-2;

对于(int i=1;i输出是什么?是显示异常还是在
imageView1
中显示黑色图像在imageView1中显示黑色图像我想你把
edgeImage1
搞砸了。检查逻辑,可能存在一些问题。此处添加了边缘检测功能,传递了40*40个图像块,并添加了2个额外的列n 2 ex非常感谢…但我有灰度图像位图.Config.ARGB_8888格式很高兴知道它解决了您的问题。现在,请将此答案标记为正确,以使其他人受益…还有一个问题,先生,如何将白色像素设置为1,黑色像素设置为0…读取像素值后显示0和非负值。。。
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class ImageProcessingActivity extends Activity {

 final static int KERNAL_WIDTH = 3;
 final static int KERNAL_HEIGHT = 3;

 int[][] kernal ={
   {0, -1, 0},
   {-1, 4, -1},
   {0, -1, 0}
 };

 ImageView imageSource, imageAfter;
 Bitmap bitmap_Source;
 ProgressBar progressBar;

 private Handler handler;
 Bitmap afterProcess;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageSource = (ImageView)findViewById(R.id.imageSource);
        imageAfter = (ImageView)findViewById(R.id.imageAfter);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);

        bitmap_Source = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        handler = new Handler();
        StratBackgroundProcess();
    }

    private void StratBackgroundProcess(){

     Runnable runnable = new Runnable(){

   @Override
   public void run() {
    afterProcess = processingBitmap(bitmap_Source, kernal);

    handler.post(new Runnable(){

     @Override
     public void run() {
      progressBar.setVisibility(View.GONE);
      imageAfter.setImageBitmap(afterProcess);
     }

    });
   }
     };
     new Thread(runnable).start();
    }

    private Bitmap processingBitmap(Bitmap src, int[][] knl){
     Bitmap dest = Bitmap.createBitmap(
       src.getWidth(), src.getHeight(), src.getConfig());

     int bmWidth = src.getWidth();
     int bmHeight = src.getHeight();
     int bmWidth_MINUS_2 = bmWidth - 2;
     int bmHeight_MINUS_2 = bmHeight - 2;

     for(int i = 1; i <= bmWidth_MINUS_2; i++){
      for(int j = 1; j <= bmHeight_MINUS_2; j++){

       //get the surround 3*3 pixel of current src[i][j] into a matrix subSrc[][]
       int[][] subSrc = new int[KERNAL_WIDTH][KERNAL_HEIGHT];
       for(int k = 0; k < KERNAL_WIDTH; k++){
        for(int l = 0; l < KERNAL_HEIGHT; l++){
         subSrc[k][l] = src.getPixel(i-1+k, j-1+l);
        }
       }

       //subSum = subSrc[][] * knl[][]
       int subSumA = 0;
       int subSumR = 0;
       int subSumG = 0;
       int subSumB = 0;

       for(int k = 0; k < KERNAL_WIDTH; k++){
        for(int l = 0; l < KERNAL_HEIGHT; l++){
         subSumR += Color.red(subSrc[k][l]) * knl[k][l];
         subSumG += Color.green(subSrc[k][l]) * knl[k][l];
         subSumB += Color.blue(subSrc[k][l]) * knl[k][l];
        }
       }

       subSumA = Color.alpha(src.getPixel(i, j));

       if(subSumR <0){
        subSumR = 0;
       }else if(subSumR > 255){
        subSumR = 255;
       }

       if(subSumG <0){
        subSumG = 0;
       }else if(subSumG > 255){
        subSumG = 255;
       }

       if(subSumB <0){
        subSumB = 0;
       }else if(subSumB > 255){
        subSumB = 255;
       }

       dest.setPixel(i, j, Color.argb(
         subSumA,
         subSumR,
         subSumG,
         subSumB));
      }
     }

     return dest;
    }

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical" >
         <TextView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="Original" />
      <ImageView
          android:id="@+id/imageSource"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_launcher"/>
      <TextView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="Result" />
      <FrameLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">
          <ImageView
              android:id="@+id/imageAfter"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
          <ProgressBar
              android:id="@+id/progressBar"
              style="?android:attr/progressBarStyleLarge"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
      </FrameLayout>

     </LinearLayout>
    </ScrollView>

</LinearLayout>