import java.awt.image.BufferedImage;
import java.awt.image.ComponentSampleModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferDouble;
import java.awt.image.DataBufferFloat;
import java.awt.image.DataBufferInt;
import java.awt.image.DataBufferShort;
import java.awt.image.DataBufferUShort;
import java.awt.image.MultiPixelPackedSampleModel;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.SinglePixelPackedSampleModel;
import android.graphics.Bitmap;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
public final class Image {
public static PixelFormat getPixelFormat(BufferedImage image) {
switch (image.get) {
case Bitmap.TYPE_3BYTE_BGR:
return PixelFormat.BGR24;
case BufferedImage.TYPE_4BYTE_ABGR:
case BufferedImage.TYPE_4BYTE_ABGR_PRE:
return PixelFormat.ABGR;
case BufferedImage.TYPE_BYTE_BINARY:
return PixelFormat.RGB444;
case BufferedImage.TYPE_BYTE_INDEXED:
return PixelFormat.RGB555;
case BufferedImage.TYPE_BYTE_GRAY:
return PixelFormat.GRAY8;
case BufferedImage.TYPE_INT_ARGB:
case BufferedImage.TYPE_INT_ARGB_PRE:
return PixelFormat.ARGB;
case BufferedImage.TYPE_INT_RGB:
return PixelFormat.RGB24;
case BufferedImage.TYPE_USHORT_GRAY:
return PixelFormat.GRAY16;
case BufferedImage.TYPE_USHORT_555_RGB:
return PixelFormat.RGB555;
case BufferedImage.TYPE_USHORT_565_RGB:
return PixelFormat.RGB565;
case BufferedImage.TYPE_CUSTOM:
return null;
default:
return null;
}
}
public static ByteBuffer createImageBuffer(Bitmap image) {
SampleModel model = image.getSampleModel();
Raster raster = image.getRaster();
DataBuffer inBuffer = raster.getDataBuffer();
ByteBuffer outBuffer = ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * model.getNumBands());
int x = -raster.getSampleModelTranslateX();
int y = -raster.getSampleModelTranslateY();
int step = model.getWidth() * model.getNumBands();
int channels = model.getNumBands();
if (model instanceof ComponentSampleModel) {
ComponentSampleModel compModel = (ComponentSampleModel) model;
step = compModel.getScanlineStride();
channels = ((ComponentSampleModel) model).getPixelStride();
}
else if (model instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel singleModel = (SinglePixelPackedSampleModel) model;
step = singleModel.getScanlineStride();
channels = 1;
}
else if (model instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel multiModel = (MultiPixelPackedSampleModel) model;
step = multiModel.getScanlineStride();
channels = ((MultiPixelPackedSampleModel) model).getPixelBitStride() / 8;
}
int start = y * step + x * channels;
if (inBuffer instanceof DataBufferByte) {
byte[] a = ((DataBufferByte) inBuffer).getData();
copy(ByteBuffer.wrap(a, start, a.length - start), step, outBuffer, step, false);
}
else if (inBuffer instanceof DataBufferShort) {
short[] a = ((DataBufferShort) inBuffer).getData();
copy(ShortBuffer.wrap(a, start, a.length - start), step, outBuffer.asShortBuffer(), step / 2, true);
}
else if (inBuffer instanceof DataBufferUShort) {
short[] a = ((DataBufferUShort) inBuffer).getData();
copy(ShortBuffer.wrap(a, start, a.length - start), step, outBuffer.asShortBuffer(), step / 2, false);
}
else if (inBuffer instanceof DataBufferInt) {
int[] a = ((DataBufferInt) inBuffer).getData();
copy(IntBuffer.wrap(a, start, a.length - start), step, outBuffer.asIntBuffer(), step / 4);
}
else if (inBuffer instanceof DataBufferFloat) {
float[] a = ((DataBufferFloat) inBuffer).getData();
copy(FloatBuffer.wrap(a, start, a.length - start), step, outBuffer.asFloatBuffer(), step / 4);
}
else if (inBuffer instanceof DataBufferDouble) {
double[] a = ((DataBufferDouble) inBuffer).getData();
copy(DoubleBuffer.wrap(a, start, a.length - start), step, outBuffer.asDoubleBuffer(), step / 8);
}
outBuffer.position(0);
return outBuffer;
}
public static Bitmap createImage(VideoFrame frame, int type) {
return createImage(frame.getData(), frame.getWidth(), frame.getHeight(), type);
}
public static Bitmap createImage(ByteBuffer data, int width, int height, int type) {
BufferedImage image = new BufferedImage(width, height, type);
SampleModel model = image.getSampleModel();
Raster raster = image.getRaster();
DataBuffer outBuffer = raster.getDataBuffer();
int x = -raster.getSampleModelTranslateX();
int y = -raster.getSampleModelTranslateY();
int step = model.getWidth() * model.getNumBands();
int channels = model.getNumBands();
data.position(0).limit(height * width * channels);
if (model instanceof ComponentSampleModel) {
ComponentSampleModel compModel = (ComponentSampleModel) model;
step = compModel.getScanlineStride();
channels = compModel.getPixelStride();
}
else if (model instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel singleModel = (SinglePixelPackedSampleModel) model;
step = singleModel.getScanlineStride();
channels = 1;
}
else if (model instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel multiModel = (MultiPixelPackedSampleModel) model;
step = multiModel.getScanlineStride();
channels = ((MultiPixelPackedSampleModel) model).getPixelBitStride() / 8;
}
int start = y * step + x * channels;
if (outBuffer instanceof DataBufferByte) {
byte[] a = ((DataBufferByte) outBuffer).getData();
copy(data, step, ByteBuffer.wrap(a, start, a.length - start), step, false);
}
else if (outBuffer instanceof DataBufferShort) {
short[] a = ((DataBufferShort) outBuffer).getData();
copy(data.asShortBuffer(), step / 2, ShortBuffer.wrap(a, start, a.length - start), step, true);
}
else if (outBuffer instanceof DataBufferUShort) {
short[] a = ((DataBufferUShort) outBuffer).getData();
copy(data.asShortBuffer(), step / 2, ShortBuffer.wrap(a, start, a.length - start), step, false);
}
else if (outBuffer instanceof DataBufferInt) {
int[] a = ((DataBufferInt) outBuffer).getData();
copy(data.asIntBuffer(), step / 4, IntBuffer.wrap(a, start, a.length - start), step);
}
else if (outBuffer instanceof DataBufferFloat) {
float[] a = ((DataBufferFloat) outBuffer).getData();
copy(data.asFloatBuffer(), step / 4, FloatBuffer.wrap(a, start, a.length - start), step);
}
else if (outBuffer instanceof DataBufferDouble) {
double[] a = ((DataBufferDouble) outBuffer).getData();
copy(data.asDoubleBuffer(), step / 8, DoubleBuffer.wrap(a, start, a.length - start), step);
}
return image;
}
}
上面这段代码是java中的代码,awt.image包只在java中有,在android中只有graphics包,不知道这些类都对应android中那些类和方法,求帮助回复!
import java.awt.image.ComponentSampleModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferDouble;
import java.awt.image.DataBufferFloat;
import java.awt.image.DataBufferInt;
import java.awt.image.DataBufferShort;
import java.awt.image.DataBufferUShort;
import java.awt.image.MultiPixelPackedSampleModel;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.SinglePixelPackedSampleModel;
import android.graphics.Bitmap;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
public final class Image {
public static PixelFormat getPixelFormat(BufferedImage image) {
switch (image.get) {
case Bitmap.TYPE_3BYTE_BGR:
return PixelFormat.BGR24;
case BufferedImage.TYPE_4BYTE_ABGR:
case BufferedImage.TYPE_4BYTE_ABGR_PRE:
return PixelFormat.ABGR;
case BufferedImage.TYPE_BYTE_BINARY:
return PixelFormat.RGB444;
case BufferedImage.TYPE_BYTE_INDEXED:
return PixelFormat.RGB555;
case BufferedImage.TYPE_BYTE_GRAY:
return PixelFormat.GRAY8;
case BufferedImage.TYPE_INT_ARGB:
case BufferedImage.TYPE_INT_ARGB_PRE:
return PixelFormat.ARGB;
case BufferedImage.TYPE_INT_RGB:
return PixelFormat.RGB24;
case BufferedImage.TYPE_USHORT_GRAY:
return PixelFormat.GRAY16;
case BufferedImage.TYPE_USHORT_555_RGB:
return PixelFormat.RGB555;
case BufferedImage.TYPE_USHORT_565_RGB:
return PixelFormat.RGB565;
case BufferedImage.TYPE_CUSTOM:
return null;
default:
return null;
}
}
public static ByteBuffer createImageBuffer(Bitmap image) {
SampleModel model = image.getSampleModel();
Raster raster = image.getRaster();
DataBuffer inBuffer = raster.getDataBuffer();
ByteBuffer outBuffer = ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * model.getNumBands());
int x = -raster.getSampleModelTranslateX();
int y = -raster.getSampleModelTranslateY();
int step = model.getWidth() * model.getNumBands();
int channels = model.getNumBands();
if (model instanceof ComponentSampleModel) {
ComponentSampleModel compModel = (ComponentSampleModel) model;
step = compModel.getScanlineStride();
channels = ((ComponentSampleModel) model).getPixelStride();
}
else if (model instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel singleModel = (SinglePixelPackedSampleModel) model;
step = singleModel.getScanlineStride();
channels = 1;
}
else if (model instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel multiModel = (MultiPixelPackedSampleModel) model;
step = multiModel.getScanlineStride();
channels = ((MultiPixelPackedSampleModel) model).getPixelBitStride() / 8;
}
int start = y * step + x * channels;
if (inBuffer instanceof DataBufferByte) {
byte[] a = ((DataBufferByte) inBuffer).getData();
copy(ByteBuffer.wrap(a, start, a.length - start), step, outBuffer, step, false);
}
else if (inBuffer instanceof DataBufferShort) {
short[] a = ((DataBufferShort) inBuffer).getData();
copy(ShortBuffer.wrap(a, start, a.length - start), step, outBuffer.asShortBuffer(), step / 2, true);
}
else if (inBuffer instanceof DataBufferUShort) {
short[] a = ((DataBufferUShort) inBuffer).getData();
copy(ShortBuffer.wrap(a, start, a.length - start), step, outBuffer.asShortBuffer(), step / 2, false);
}
else if (inBuffer instanceof DataBufferInt) {
int[] a = ((DataBufferInt) inBuffer).getData();
copy(IntBuffer.wrap(a, start, a.length - start), step, outBuffer.asIntBuffer(), step / 4);
}
else if (inBuffer instanceof DataBufferFloat) {
float[] a = ((DataBufferFloat) inBuffer).getData();
copy(FloatBuffer.wrap(a, start, a.length - start), step, outBuffer.asFloatBuffer(), step / 4);
}
else if (inBuffer instanceof DataBufferDouble) {
double[] a = ((DataBufferDouble) inBuffer).getData();
copy(DoubleBuffer.wrap(a, start, a.length - start), step, outBuffer.asDoubleBuffer(), step / 8);
}
outBuffer.position(0);
return outBuffer;
}
public static Bitmap createImage(VideoFrame frame, int type) {
return createImage(frame.getData(), frame.getWidth(), frame.getHeight(), type);
}
public static Bitmap createImage(ByteBuffer data, int width, int height, int type) {
BufferedImage image = new BufferedImage(width, height, type);
SampleModel model = image.getSampleModel();
Raster raster = image.getRaster();
DataBuffer outBuffer = raster.getDataBuffer();
int x = -raster.getSampleModelTranslateX();
int y = -raster.getSampleModelTranslateY();
int step = model.getWidth() * model.getNumBands();
int channels = model.getNumBands();
data.position(0).limit(height * width * channels);
if (model instanceof ComponentSampleModel) {
ComponentSampleModel compModel = (ComponentSampleModel) model;
step = compModel.getScanlineStride();
channels = compModel.getPixelStride();
}
else if (model instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel singleModel = (SinglePixelPackedSampleModel) model;
step = singleModel.getScanlineStride();
channels = 1;
}
else if (model instanceof MultiPixelPackedSampleModel) {
MultiPixelPackedSampleModel multiModel = (MultiPixelPackedSampleModel) model;
step = multiModel.getScanlineStride();
channels = ((MultiPixelPackedSampleModel) model).getPixelBitStride() / 8;
}
int start = y * step + x * channels;
if (outBuffer instanceof DataBufferByte) {
byte[] a = ((DataBufferByte) outBuffer).getData();
copy(data, step, ByteBuffer.wrap(a, start, a.length - start), step, false);
}
else if (outBuffer instanceof DataBufferShort) {
short[] a = ((DataBufferShort) outBuffer).getData();
copy(data.asShortBuffer(), step / 2, ShortBuffer.wrap(a, start, a.length - start), step, true);
}
else if (outBuffer instanceof DataBufferUShort) {
short[] a = ((DataBufferUShort) outBuffer).getData();
copy(data.asShortBuffer(), step / 2, ShortBuffer.wrap(a, start, a.length - start), step, false);
}
else if (outBuffer instanceof DataBufferInt) {
int[] a = ((DataBufferInt) outBuffer).getData();
copy(data.asIntBuffer(), step / 4, IntBuffer.wrap(a, start, a.length - start), step);
}
else if (outBuffer instanceof DataBufferFloat) {
float[] a = ((DataBufferFloat) outBuffer).getData();
copy(data.asFloatBuffer(), step / 4, FloatBuffer.wrap(a, start, a.length - start), step);
}
else if (outBuffer instanceof DataBufferDouble) {
double[] a = ((DataBufferDouble) outBuffer).getData();
copy(data.asDoubleBuffer(), step / 8, DoubleBuffer.wrap(a, start, a.length - start), step);
}
return image;
}
}
上面这段代码是java中的代码,awt.image包只在java中有,在android中只有graphics包,不知道这些类都对应android中那些类和方法,求帮助回复!