下面列出了android.graphics.Matrix#setPolyToPoly ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Matrix getPreviewTransform(int currentDisplayOrientation,
RectF surfaceDimensions,
RectF desiredBounds) {
if (!orientationIsValid(currentDisplayOrientation)) {
return new Matrix();
}
// The system transparently transforms the image to fill the surface
// when the device is in its natural orientation. We rotate the
// coordinates of the rectangle's corners to be relative to the
// original image, instead of to the current screen orientation.
float[] surfacePolygon = rotate(convertRectToPoly(surfaceDimensions),
2 * currentDisplayOrientation / 90);
float[] desiredPolygon = convertRectToPoly(desiredBounds);
Matrix transform = new Matrix();
// Use polygons instead of rectangles so that rotation will be
// calculated, since that is not done by the new camera API.
transform.setPolyToPoly(surfacePolygon, 0, desiredPolygon, 0, 4);
return transform;
}
private void ployTWO(Canvas canvas) {
canvas.save();
float[] src = {0, 0, // 左上
bt.getWidth(), 0, // 右上
bt.getWidth(), bt.getHeight(), // 右下
0, bt.getHeight()}; // 左下
float[] dst = {0, 0, // 左上
bt.getWidth(),50, // 右上
bt.getWidth(), bt.getHeight() -50, // 右下
0, bt.getHeight()}; // 左下
Matrix ploy=new Matrix();
ploy.setPolyToPoly(src,0,dst,0,2);
canvas.drawBitmap(bt,ploy, paint);
canvas.restore();
}
private void ployOne(Canvas canvas) {
canvas.save();
float[] src = {0, 0, // 左上
bt.getWidth(), 0, // 右上
bt.getWidth(), bt.getHeight(), // 右下
0, bt.getHeight()}; // 左下
float[] dst = {0+100, 0, // 左上
bt.getWidth(),50, // 右上
bt.getWidth(), bt.getHeight() -50, // 右下
0, bt.getHeight()}; // 左下
Matrix ploy=new Matrix();
ploy.setPolyToPoly(src,0,dst,0,1);
canvas.drawBitmap(bt,ploy, paint);
canvas.restore();
}
/**
* Configures the transform matrix for TextureView based on {@link #mDisplayOrientation} and
* the surface size.
*/
void configureTransform() {
Matrix matrix = new Matrix();
if (mDisplayOrientation % 180 == 90) {
final int width = getWidth();
final int height = getHeight();
// Rotate the camera preview when the screen is landscape.
matrix.setPolyToPoly(
new float[]{
0.f, 0.f, // top left
width, 0.f, // top right
0.f, height, // bottom left
width, height, // bottom right
}, 0,
mDisplayOrientation == 90 ?
// Clockwise
new float[]{
0.f, height, // top left
0.f, 0.f, // top right
width, height, // bottom left
width, 0.f, // bottom right
} : // mDisplayOrientation == 270
// Counter-clockwise
new float[]{
width, 0.f, // top left
width, height, // top right
0.f, 0.f, // bottom left
0.f, height, // bottom right
}, 0,
4);
} else if (mDisplayOrientation == 180) {
matrix.postRotate(180, getWidth() / 2, getHeight() / 2);
}
mTextureView.setTransform(matrix);
}
/**
* Configures the transform matrix for TextureView based on {@link #mDisplayOrientation} and
* the surface size.
*/
void configureTransform() {
Matrix matrix = new Matrix();
if (mDisplayOrientation % 180 == 90) {
final int width = getWidth();
final int height = getHeight();
// Rotate the camera preview when the screen is landscape.
matrix.setPolyToPoly(
new float[]{
0.f, 0.f, // top left
width, 0.f, // top right
0.f, height, // bottom left
width, height, // bottom right
}, 0,
mDisplayOrientation == 90 ?
// Clockwise
new float[]{
0.f, height, // top left
0.f, 0.f, // top right
width, height, // bottom left
width, 0.f, // bottom right
} : // mDisplayOrientation == 270
// Counter-clockwise
new float[]{
width, 0.f, // top left
width, height, // top right
0.f, 0.f, // bottom left
0.f, height, // bottom right
}, 0,
4);
} else if (mDisplayOrientation == 180) {
matrix.postRotate(180, getWidth() / 2, getHeight() / 2);
}
mTextureView.setTransform(matrix);
}
/**
* Configures the transform matrix for TextureView based on {@link #mDisplayOrientation} and
* the surface size.
*/
void configureTransform() {
Matrix matrix = new Matrix();
if (mDisplayOrientation % 180 == 90) {
final int width = getWidth();
final int height = getHeight();
// Rotate the camera preview when the screen is landscape.
matrix.setPolyToPoly(
new float[]{
0.f, 0.f, // top left
width, 0.f, // top right
0.f, height, // bottom left
width, height, // bottom right
}, 0,
mDisplayOrientation == 90 ?
// Clockwise
new float[]{
0.f, height, // top left
0.f, 0.f, // top right
width, height, // bottom left
width, 0.f, // bottom right
} : // mDisplayOrientation == 270
// Counter-clockwise
new float[]{
width, 0.f, // top left
width, height, // top right
0.f, 0.f, // bottom left
0.f, height, // bottom right
}, 0,
4);
} else if (mDisplayOrientation == 180) {
matrix.postRotate(180, getWidth() / 2, getHeight() / 2);
}
mTextureView.setTransform(matrix);
}
/**
* Configures the transform matrix for TextureView based on {@link #mDisplayOrientation} and
* the surface size.
*/
public void configureTransform() {
Matrix matrix = new Matrix();
if (mDisplayOrientation % 180 == 90) {
final int width = getWidth();
final int height = getHeight();
// Rotate the camera preview when the screen is landscape.
matrix.setPolyToPoly(
new float[]{
0.f, 0.f, // top left
width, 0.f, // top right
0.f, height, // bottom left
width, height, // bottom right
}, 0,
mDisplayOrientation == 90 ?
// Clockwise
new float[]{
0.f, height, // top left
0.f, 0.f, // top right
width, height, // bottom left
width, 0.f, // bottom right
}
: // mDisplayOrientation == 270
// Counter-clockwise
new float[]{
width, 0.f, // top left
width, height, // top right
0.f, 0.f, // bottom left
0.f, height, // bottom right
}, 0,
4);
}
mTextureView.setTransform(matrix);
}
public Bitmap getTransformedBitmap(byte[] data) {
Log.d(TAG, "Picture taken!!");
Bitmap b = rotateBitmap(BitmapFactory.decodeByteArray(data, 0, data.length), mCameraRotation);
int w = b.getWidth();
int h = b.getHeight();
int sw = mCameraPreview.getWidth();
int sh = mCameraPreview.getHeight();
/**
* Map screen coordinates to image coordinates
* Assumes aspect ratios of preview and image are the same
*/
Point[] coords = mHLView.getCoords();
float[] src = new float[coords.length * 2];
for (int i = 0; i < coords.length; i++) {
Point coord = coords[i];
src[i * 2 + 0] = (coord.x / (float) sw) * w;
src[i * 2 + 1] = (coord.y / (float) sh) * h;
}
Bitmap target = Bitmap.createBitmap(b.getWidth(), b.getHeight(), b.getConfig());
int tw = target.getWidth();
int th = target.getHeight();
Canvas canvas = new Canvas(target);
Matrix m = new Matrix();
m.setPolyToPoly(src, 0, new float[]{0, 0, tw, 0, tw, th, 0, th}, 0, 4);
canvas.drawBitmap(b, m, null);
return target;
}
/**
* Configures the transform matrix for TextureView based on {@link #mDisplayOrientation} and
* the surface size.
*/
void configureTransform() {
Matrix matrix = new Matrix();
if (mDisplayOrientation % 180 == 90) {
final int width = getWidth();
final int height = getHeight();
// Rotate the camera preview when the screen is landscape.
matrix.setPolyToPoly(
new float[]{
0.f, 0.f, // top left
width, 0.f, // top right
0.f, height, // bottom left
width, height, // bottom right
}, 0,
mDisplayOrientation == 90 ?
// Clockwise
new float[]{
0.f, height, // top left
0.f, 0.f, // top right
width, height, // bottom left
width, 0.f, // bottom right
} : // mDisplayOrientation == 270
// Counter-clockwise
new float[]{
width, 0.f, // top left
width, height, // top right
0.f, 0.f, // bottom left
0.f, height, // bottom right
}, 0,
4);
} else if (mDisplayOrientation == 180) {
matrix.postRotate(180, getWidth() / 2, getHeight() / 2);
}
mTextureView.setTransform(matrix);
}