Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android位图到BuffereImage_Java_Android_Bitmap_Image Manipulation - Fatal编程技术网

Java Android位图到BuffereImage

Java Android位图到BuffereImage,java,android,bitmap,image-manipulation,Java,Android,Bitmap,Image Manipulation,有没有简单的方法将位图图像数据类型转换为BuffereImage 我需要一个缩放到224*224的位图图像转换成一个BuffereImage,以便在两个BuffereImage之间进行像素比较 我一直在尝试开发一个简单的面部识别类,该类将获取2张位图图像(取自android摄像头),并使用本地二进制模式识别算法进行比较。图像比较的源代码: import java.awt.image.*; import java.awt.color.ColorSpace; public class ImageE

有没有简单的方法将位图图像数据类型转换为BuffereImage

我需要一个缩放到224*224的位图图像转换成一个BuffereImage,以便在两个BuffereImage之间进行像素比较

我一直在尝试开发一个简单的面部识别类,该类将获取2张位图图像(取自android摄像头),并使用本地二进制模式识别算法进行比较。图像比较的源代码:

import java.awt.image.*;
import java.awt.color.ColorSpace;

public class ImageEncode {

 public static boolean facialRecognition(BufferedImage i, BufferedImage i2) {

  int currentPixelValue, newPixelValue;
  int[][] imageArray = new int[224][224], imageArray2 = new int[224][224], lbpArray = new int[224][224], lbpArray2 = new int[224][224], histogram = new int[9][256], histogram2 = new int[9][256];



  //input pictures, resize to 224x224
  BufferedImage image = i;
  BufferedImage image2 = i2;

  //convert to gray scale
  ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
  ColorConvertOp op = new ColorConvertOp(cs, null);
  image = op.filter(image, null);
  image2=op.filter(image2, null);

  //gaussian filter
  Kernel kernel = new Kernel(3,3,
    new float[] {
     1f/9f, 1f/9f, 1f/9f,
     1f/9f, 1f/9f, 1f/9f,
     1f/9f, 1f/9f, 1f/9f});
  BufferedImageOp op2 = new ConvolveOp(kernel);
  image = op2.filter(image, null);
  image2= op2.filter(image2, null);

  //convert images to pixel value array
  for(int row=0; row<=223; row++){
   for(int col=0; col<=223; col++){
    imageArray[row][col]=image.getRGB(row, col);
    imageArray2[row][col]=image2.getRGB(row, col);
   }
  }  

  //perform lbp calculations  
  for(int row=1; row<223; row++){
   for(int col=1; col<223; col++){
    currentPixelValue=imageArray[row][col];
    newPixelValue=0;
    if(imageArray[row-1][col-1]>currentPixelValue) newPixelValue=newPixelValue+1;
    if(imageArray[row-1][col]>currentPixelValue) newPixelValue=newPixelValue+2;
    if(imageArray[row-1][col+1]>currentPixelValue) newPixelValue=newPixelValue+4;
    if(imageArray[row][col+1]>currentPixelValue) newPixelValue=newPixelValue+8;
    if(imageArray[row+1][col+1]>currentPixelValue) newPixelValue=newPixelValue+16;
    if(imageArray[row+1][col]>currentPixelValue) newPixelValue=newPixelValue+32;
    if(imageArray[row+1][col-1]>currentPixelValue) newPixelValue=newPixelValue+64;
    if(imageArray[row][col-1]>currentPixelValue) newPixelValue=newPixelValue+128;
    lbpArray[row][col]=newPixelValue;
   }
  }

  for(int row=1; row<223; row++){
   for(int col=1; col<223; col++){
    currentPixelValue=imageArray2[row][col];
    newPixelValue=0;
    if(imageArray2[row-1][col-1]>currentPixelValue) newPixelValue=newPixelValue+1;
    if(imageArray2[row-1][col]>currentPixelValue) newPixelValue=newPixelValue+2;
    if(imageArray2[row-1][col+1]>currentPixelValue) newPixelValue=newPixelValue+4;
    if(imageArray2[row][col+1]>currentPixelValue) newPixelValue=newPixelValue+8;
    if(imageArray2[row+1][col+1]>currentPixelValue) newPixelValue=newPixelValue+16;
    if(imageArray2[row+1][col]>currentPixelValue) newPixelValue=newPixelValue+32;
    if(imageArray2[row+1][col-1]>currentPixelValue) newPixelValue=newPixelValue+64;
    if(imageArray2[row][col-1]>currentPixelValue) newPixelValue=newPixelValue+128;
    lbpArray2[row][col]=newPixelValue;
   }
  }

  //create histograms
  for(int row=1; row<=222; row++){
   for(int col=1; col<=222; col++){
    if(row<75 && col<75) histogram[0][imageArray[row][col]]++;
    if(row<75 && col>74 && col<149) histogram[1][imageArray[row][col]]++;
    if(row<75 && col>148 && col<223) histogram[2][imageArray[row][col]]++;
    if(row>74 && row<149 && col<75) histogram[3][imageArray[row][col]]++;
    if(row>74 && row<149 && col>75 && col<149) histogram[4][imageArray[row][col]]++;
    if(row>74 && row<149 && col>148 && col<223) histogram[5][imageArray[row][col]]++;
    if(row>148 && row<223 && col<75) histogram[6][imageArray[row][col]]++;
    if(row>148 && row<223 && col>74 && col<149) histogram[7][imageArray[row][col]]++;
    if(row>148 && row<223 && col>148 && col<223) histogram[8][imageArray[row][col]]++;
   }
  }  

  for(int row=1; row<=222; row++){
   for(int col=1; col<=222; col++){
    if(row<75 && col<75) histogram2[0][imageArray2[row][col]]++;
    if(row<75 && col>74 && col<149) histogram2[1][imageArray2[row][col]]++;
    if(row<75 && col>148 && col<223) histogram2[2][imageArray2[row][col]]++;
    if(row>74 && row<149 && col<75) histogram2[3][imageArray2[row][col]]++;
    if(row>74 && row<149 && col>75 && col<149) histogram2[4][imageArray2[row][col]]++;
    if(row>74 && row<149 && col>148 && col<223) histogram2[5][imageArray2[row][col]]++;
    if(row>148 && row<223 && col<75) histogram2[6][imageArray2[row][col]]++;
    if(row>148 && row<223 && col>74 && col<149) histogram2[7][imageArray2[row][col]]++;
    if(row>148 && row<223 && col>148 && col<223) histogram2[8][imageArray2[row][col]]++;
   }
  }  

  //Compare histograms
  for(int k=0; k<=8; k++){
   for(int j=0; j<=255; j++){
    if((((histogram[k][j])*0.1)+histogram[k][j]) < histogram2[k][j] || (histogram[k][j]-((histogram[k][j])*0.1)) > histogram2[k][j]){
     return false;
    }
   }
  }

  return true;

 }

}
导入java.awt.image.*;
导入java.awt.color.ColorSpace;
公共类图像编码{
公共静态布尔面部识别(BuffereImage i、BuffereImage i2){
int currentPixelValue,newPixelValue;
int[]imageArray=new int[224][224],imageArray2=new int[224][224],lbpArray=new int[224][224],lbpArray2=new int[224][224],histogram=new int[9][256],histogram2=new int[9][256];
//输入图片,调整大小为224x224
BuffereImage图像=i;
BuffereImage image2=i2;
//转换为灰度
ColorSpace cs=ColorSpace.getInstance(ColorSpace.cs_GRAY);
ColorConvertOp=新的ColorConvertOp(cs,null);
image=op.filter(image,null);
image2=op.filter(image2,null);
//高斯滤波器
内核=新内核(3,3,
新浮动[]{
1f/9f、1f/9f、1f/9f、,
1f/9f、1f/9f、1f/9f、,
1f/9f、1f/9f、1f/9f});
BufferedImageOp op2=新卷积运算(内核);
image=op2.filter(image,null);
image2=op2.filter(image2,null);
//将图像转换为像素值数组
对于(int row=0;rowcurrentPixelValue)newPixelValue=newPixelValue+8;
如果(imageArray[row+1][col+1]>currentPixelValue)newPixelValue=newPixelValue+16;
如果(imageArray[row+1][col]>currentPixelValue)newPixelValue=newPixelValue+32;
如果(imageArray[row+1][col-1]>currentPixelValue)newPixelValue=newPixelValue+64;
如果(imageArray[row][col-1]>currentPixelValue)newPixelValue=newPixelValue+128;
lbpArray[row][col]=新像素值;
}
}
对于(int row=1;rowcurrentPixelValue)newPixelValue=newPixelValue+2;
如果(imageArray2[row-1][col+1]>currentPixelValue)newPixelValue=newPixelValue+4;
如果(imageArray2[行][col+1]>currentPixelValue)newPixelValue=newPixelValue+8;
如果(imageArray2[row+1][col+1]>currentPixelValue)newPixelValue=newPixelValue+16;
如果(imageArray2[row+1][col]>currentPixelValue)newPixelValue=newPixelValue+32;
如果(imageArray2[row+1][col-1]>currentPixelValue)newPixelValue=newPixelValue+64;
如果(imageArray2[行][col-1]>currentPixelValue)newPixelValue=newPixelValue+128;
lbpArray2[row][col]=新像素值;
}
}
//创建直方图

对于(int row=1;row,我已经多次使用该类将不同的位图转换为BuffereImage

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import java.io.InputStream;

/**
 * Utility class for loading windows bitmap files
 * <p>
 * Based on code from author Abdul Bezrati and Pepijn Van Eeckhoudt
 */
public class BitmapLoader {

    /**
     * Static method to load a bitmap file based on the filename passed in.
     * Based on the bit count, this method will either call the 8 or 24 bit
     * bitmap reader methods
     *
     * @param file The name of the bitmap file to read
     * @throws IOException
     * @return A BufferedImage of the bitmap
     */
    public static BufferedImage loadBitmap(String file) throws IOException {
        BufferedImage image;
        InputStream input = null;
        try {
            input = ResourceRetriever.getResourceAsStream(file);

            int bitmapFileHeaderLength = 14;
            int bitmapInfoHeaderLength = 40;

            byte bitmapFileHeader[] = new byte[bitmapFileHeaderLength];
            byte bitmapInfoHeader[] = new byte[bitmapInfoHeaderLength];

            input.read(bitmapFileHeader, 0, bitmapFileHeaderLength);
            input.read(bitmapInfoHeader, 0, bitmapInfoHeaderLength);

            int nSize = bytesToInt(bitmapFileHeader, 2);
            int nWidth = bytesToInt(bitmapInfoHeader, 4);
            int nHeight = bytesToInt(bitmapInfoHeader, 8);
            int nBiSize = bytesToInt(bitmapInfoHeader, 0);
            int nPlanes = bytesToShort(bitmapInfoHeader, 12);
            int nBitCount = bytesToShort(bitmapInfoHeader, 14);
            int nSizeImage = bytesToInt(bitmapInfoHeader, 20);
            int nCompression = bytesToInt(bitmapInfoHeader, 16);
            int nColoursUsed = bytesToInt(bitmapInfoHeader, 32);
            int nXPixelsMeter = bytesToInt(bitmapInfoHeader, 24);
            int nYPixelsMeter = bytesToInt(bitmapInfoHeader, 28);
            int nImportantColours = bytesToInt(bitmapInfoHeader, 36);

            if (nBitCount == 24) {
                image = read24BitBitmap(nSizeImage, nHeight, nWidth, input);
            } else if (nBitCount == 8) {
                image = read8BitBitmap(nColoursUsed, nBitCount, nSizeImage, nWidth, nHeight, input);
            } else {
                System.out.println("Not a 24-bit or 8-bit Windows Bitmap, aborting...");
                image = null;
            }
        } finally {
            try {
                if (input != null)
                    input.close();
            } catch (IOException e) {
            }
        }
        return image;
    }

    /**
     * Static method to read a 8 bit bitmap
     *
     * @param nColoursUsed Number of colors used
     * @param nBitCount The bit count
     * @param nSizeImage The size of the image in bytes
     * @param nWidth The width of the image
     * @param input The input stream corresponding to the image
     * @throws IOException
     * @return A BufferedImage of the bitmap
     */
    private static BufferedImage read8BitBitmap(int nColoursUsed, int nBitCount, int nSizeImage, int nWidth, int nHeight, InputStream input) throws IOException {
        int nNumColors = (nColoursUsed > 0) ? nColoursUsed : (1 & 0xff) << nBitCount;

        if (nSizeImage == 0) {
            nSizeImage = ((((nWidth * nBitCount) + 31) & ~31) >> 3);
            nSizeImage *= nHeight;
        }

        int npalette[] = new int[nNumColors];
        byte bpalette[] = new byte[nNumColors * 4];
        readBuffer(input, bpalette);
        int nindex8 = 0;

        for (int n = 0; n < nNumColors; n++) {
            npalette[n] = (255 & 0xff) << 24 |
                    (bpalette[nindex8 + 2] & 0xff) << 16 |
                    (bpalette[nindex8 + 1] & 0xff) << 8 |
                    (bpalette[nindex8 + 0] & 0xff);

            nindex8 += 4;
        }

        int npad8 = (nSizeImage / nHeight) - nWidth;
        BufferedImage bufferedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_ARGB);
        DataBufferInt dataBufferByte = ((DataBufferInt) bufferedImage.getRaster().getDataBuffer());
        int[][] bankData = dataBufferByte.getBankData();
        byte bdata[] = new byte[(nWidth + npad8) * nHeight];

        readBuffer(input, bdata);
        nindex8 = 0;

        for (int j8 = nHeight - 1; j8 >= 0; j8--) {
            for (int i8 = 0; i8 < nWidth; i8++) {
                bankData[0][j8 * nWidth + i8] = npalette[((int) bdata[nindex8] & 0xff)];
                nindex8++;
            }
            nindex8 += npad8;
        }

        return bufferedImage;
    }

    /**
     * Static method to read a 24 bit bitmap
     *
     * @param nSizeImage size of the image  in bytes
     * @param nHeight The height of the image
     * @param nWidth The width of the image
     * @param input The input stream corresponding to the image
     * @throws IOException
     * @return A BufferedImage of the bitmap
     */
    private static BufferedImage read24BitBitmap(int nSizeImage, int nHeight, int nWidth, InputStream input) throws IOException {
        int npad = (nSizeImage / nHeight) - nWidth * 3;
        if (npad == 4 || npad < 0)
            npad = 0;
        int nindex = 0;
        BufferedImage bufferedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_4BYTE_ABGR);
        DataBufferByte dataBufferByte = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer());
        byte[][] bankData = dataBufferByte.getBankData();
        byte brgb[] = new byte[(nWidth + npad) * 3 * nHeight];

        readBuffer(input, brgb);

        for (int j = nHeight - 1; j >= 0; j--) {
            for (int i = 0; i < nWidth; i++) {
                int base = (j * nWidth + i) * 4;
                bankData[0][base] = (byte) 255;
                bankData[0][base + 1] = brgb[nindex];
                bankData[0][base + 2] = brgb[nindex + 1];
                bankData[0][base + 3] = brgb[nindex + 2];
                nindex += 3;
            }
            nindex += npad;
        }

        return bufferedImage;
    }

    /**
     * Converts bytes to an int
     *
     * @param bytes An array of bytes
     * @param index
     * @returns A int representation of the bytes
     */
    private static int bytesToInt(byte[] bytes, int index) {
        return (bytes[index + 3] & 0xff) << 24 |
                (bytes[index + 2] & 0xff) << 16 |
                (bytes[index + 1] & 0xff) << 8 |
                bytes[index + 0] & 0xff;
    }

    /**
     * Converts bytes to a short
     *
     * @param bytes An array of bytes
     * @param index
     * @returns A short representation of the bytes
     */
    private static short bytesToShort(byte[] bytes, int index) {
        return (short) (((bytes[index + 1] & 0xff) << 8) |
                (bytes[index + 0] & 0xff));
    }

    /**
     * Reads the buffer
     *
     * @param in An InputStream
     * @param buffer An array of bytes
     * @throws IOException
     */
    private static void readBuffer(InputStream in, byte[] buffer) throws IOException {
        int bytesRead = 0;
        int bytesToRead = buffer.length;
        while (bytesToRead > 0) {
            int read = in.read(buffer, bytesRead, bytesToRead);
            bytesRead += read;
            bytesToRead -= read;
        }
    }
}
导入java.awt.image.buffereImage;
导入java.awt.image.DataBufferByte;
导入java.awt.image.DataBufferInt;
导入java.io.IOException;
导入java.io.InputStream;
/**
*用于加载windows位图文件的实用程序类
*
*基于作者Abdul Bezrati和Pepijn Van Eeckhoudt的代码
*/
公共类位图加载器{
/**
*静态方法,根据传入的文件名加载位图文件。
*基于位计数,此方法将调用8位或24位
*位图读取器方法
*
*@param file要读取的位图文件的名称
*@抛出异常
*@返回位图的BuffereImage
*/
公共静态缓冲区映像加载位图(字符串文件)引发IOException{
缓冲图像;
InputStream输入=null;
试一试{
input=ResourceRetriever.getResourceAsStream(文件);
int bitmapFileHeaderLength=14;
int bitmapinfo headerlength=40;
字节bitmapFileHeader[]=新字节[bitmapFileHeaderLength];
字节BitMapInfo头[]=新字节[BitMapInfo头长度];
input.read(bitmapFileHeader,0,bitmapFileHeaderLength);
读取(BitMapInfo头,0,BitMapInfo头长度);
int nSize=bytestpoint(bitmapFileHeader,2);
int nWidth=bytestpoint(BitMapInfo头,4);
int nHeight=bytestpoint(BitMapInfo头,8);
int nBiSize=bytestpoint(BitMapInfo头,0);
int nPlanes=bytesToShort(BitMapInfo头,12);
int nBitCount=bytesToShort(BitMapInfo头,14);
int nSizeImage=bytestpoint(BitMapInfo头,20);
int nCompression=ByTestPoint(BitMapInfo头,16);
int nColoursUsed=ByTestPoint(BitMapInfo头,32);
int nXPixelsMeter=bytestpoint(BitMapInfo头,24);
int-nYPixelsMeter=bytestpoint(BitMapInfo头,28);
int nimportantcolors=bytestpoint(BitMapInfo头,36);
如果(nBitCount==24){
image=read24位位图(nSizeImage、nHeight、nWidth、input);
}否则如果(nBitCount==8){
image=read8bit位图(n使用的颜色、nBitCount、nSizeImage、nWidth、nHeight、输入);
}否则{
System.out.println(“不是24位或8位Windows位图,正在中止…”);
image=null;
}
}最后{
试一试{
如果(输入!=null)
input.close();
}捕获(IOE异常){
}
}
返回图像;
}
/**
*读取8位位图的静态方法
*
*@param nColoursUsed使用的颜色数
*@param nBitCount位计数
*@param nSizeImage图像的大小(以字节为单位)
*@param nWidth图像的宽度
*@param输入图像对应的输入流
*@抛出异常
*@返回位图的BuffereImage
*/
私有静态缓冲区映像read8BitBitmap(int-nColoursUsed、int-nBitCount、int-nSizeImage、int-nWidth、int-nHeight、InputStream输入)引发IOException{
int nNumColors=(nColoursUsed>0)?nColoursUsed:(1&0xff)>3);
nSizeImage*=nHeight;
}