感谢您的阅读,几天来一直在努力解决这个问题,并尽可能多地搜索代码。正如您将在下面看到的,我尝试了多种方法。
我正在使用第 3 方设备,它返回大小为 152 x 500 的位图。左侧垂直下方有一条黑色条带,宽度为 24。所以实际上我需要的是一个 128 x 500 的尺寸,它从左边开始 24 英寸。
无论我尝试什么,当图像被保存时,我似乎都无法摆脱那个黑条。
这是代码,对不起,太多了,但我想我会全部展示出来。寻找
// 点击扫描按钮并记录 Log.e("tag", "Wait For Scan Capture");
非常感谢您对此的任何帮助。
public class ImageCaptureActivity extends Activity implements OnClickListener {
private LinearLayout ll_root;
private Button btn_scan;
private Button btn_process;
private LinearLayout mLayout;
private Button mButton;
private FingerView mImageView;
private FPCDevKitInterface mFPCDevKit;
public View minutiaView;
public Bitmap tempBitmap;
public Bitmap scanBitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_login_scan);
Log.e("tag", "0");
//ll_root = (LinearLayout)findViewById(R.id.ll_root);
btn_scan = (Button)findViewById(R.id.scan);
btn_process = (Button)findViewById(R.id.proceed_button);
Log.e("tag", "1");
// mFPCDevKit = new SSDDevice ((UsbManager) getSystemService(Context.USB_SERVICE));
// mImageView = new FingerView(this, mFPCDevKit.createCompatibleBitmap());
// mImageView.setLayoutParams(new LinearLayout.LayoutParams(200,200));
//add view here.
// ll_root.addView(mImageView);
// Just keep it commented, because we cannot find the id of ll_root. it is currently null.
Log.i("tag", "2");
Log.e("tag", "5");
/* ---------------------------------------------------------------------------------------------------------------*/
/* Following is the initial Screen Layout Section done as a Setup of the screen and Before the Capture is clicked */
// Manually set up the screen layout
mLayout = new LinearLayout(this);
mLayout.setOrientation(LinearLayout.VERTICAL);
// Manually create the Button
mButton = new Button(this);
mButton.setOnClickListener(this);
mButton.setText("Capture Image");
// Setup the FPC Service to get the finger scan from the device
Log.e("tag", "Setup FPC");
mFPCDevKit = new SSDDevice ((UsbManager) getSystemService(Context.USB_SERVICE));
// Call the FingerView function and then load the bitmap into mImageView
Log.e("tag", "Load into mImageView");
mImageView = new FingerView(this, mFPCDevKit.createCompatibleBitmap());
// Modify the viewing area for the scanned image size
mImageView.setLayoutParams(new LinearLayout.LayoutParams(24+128,500)); // Left area plus image width and crop of right area
// Add the Button and the mImageView to the Layout
Log.e("tag", "Add Views");
mLayout.addView(mButton);
mLayout.addView(mImageView);
// Display the screen
Log.e("tag", "Display Screen");
setContentView(mLayout);
// setContentView(R.layout.my_login_scan);
/* ----------- End Screen Layout Setup ------------------ */
}
public static AlertDialog createCapturePromtDialog(Context context,
DevKitType type) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (type == DevKitType.ASD)
builder.setTitle("Place finger on sensor");
else
builder.setTitle("Swipe finger over sensor");
return builder.create();
}
private class FingerView extends View {
public Bitmap mBitmap;
private Matrix mMatrix;
public FingerView(Context context, Bitmap bitmap) {
super(context);
mBitmap = bitmap;
mMatrix = new Matrix();
setBackgroundColor(Color.WHITE); // is the full screen area background colour as well as the template area colour
setDrawingCacheEnabled(true);
Log.e("tag", "FingerView");
}
public Bitmap getBitmap() {
return mBitmap;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Log.e("onSizeChanged", "w = " + w + " h = " + h + " oldw = " + oldw
+ " oldh = " + oldh);
// This use to Scale the current bitmap fill the screen.
// The last variable: Matrix.ScaleToFit.FILL for fill two dimensions
// Matrix.ScaleToFit.CENTER scale to center and keep the
// width/height ratio.
//mMatrix.setRectToRect(
// new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight()),
// new RectF(0, 0, w, h), Matrix.ScaleToFit.CENTER);
super.onSizeChanged(w, h, oldw, oldh);
// Lets try to resize to 150 x 500 - Harry July 14 2013
// mMatrix.setRectToRect(new RectF(0, 0, 128, 500),new RectF(-24, 0, 150, 500), Matrix.ScaleToFit.CENTER);
Log.e("tag", "Inside onsizechanged " + 0);
/* Scales image to fit into a new rectangle imageView
Matrix m = imageView.getImageMatrix();
RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
RectF viewRect = new RectF(0, 0, imageView.getWidth(), imageView.getHeight());
m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
imageView.setImageMatrix(m);
*/
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
Log.e("onDraw Begin","");
canvas.drawBitmap(mBitmap, mMatrix, null);
super.onDraw(canvas);
}
}
// Scan Button Clicked
public void onClick(View v) {
Log.e("tag", "onClick happened");
// Erase the scan area to White
mImageView.getBitmap().eraseColor(Color.WHITE); // Doesn't seem to make a difference
// Bitmap tempBitmap = Bitmap.createBitmap(128+24+24,500,Bitmap.Config.ARGB_8888);
Log.e("onClick Enter second one= " + 0,
"w = " + 0 + " h =" + 0);
try {
// Wait for the scanner and then load the image into mImageView.mBitmap
mFPCDevKit.clearAbort();
Log.e("tag", "Wait For Scan Capture");
scanBitmap = mFPCDevKit.createCompatibleBitmap();
mFPCDevKit.waitAndCaptureImage(mImageView.getBitmap()); // Image displays in mImageView
//mFPCDevKit.waitAndCaptureImage(scanBitmap); // Tried to put image into a Bitmap
//Create a new image bitmap and attach a brand new canvas to it
// Bitmap minBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(scanBitmap);
//Draw the image bitmap into the canvas
// tempCanvas.drawBitmap(scanBitmap, 0, 0, null);
//Draw everything else you want into the canvas, in this example a rectangle with rounded edges
// tempCanvas.drawRoundRect(new RectF(x1,y1,x2,y2), 2, 2, scanBitmap);
//Attach the canvas to the ImageView
// mImageView.setBackgroundDrawable(new BitmapDrawable(getResources(), scanBitmap));
// @Override protected void onDraw(Canvas canvas) { canvas.drawColor(0xFFAAAAAA); canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); if(item_id==3){ canvas.drawText(AppModel.contents, 100, 100, mPaint); } else{ canvas.drawPath(mPath, mPaint); } }
// Bitmap mmBitmap = Bitmap.createBitmap(mImageView.mBitmap, 22, 0, mImageView.mBitmap.getWidth()-100, mImageView.mBitmap.getHeight());
// Canvas canvas1 = new Canvas(mmBitmap);
// mImageView.setImageBitmap(tempBitmap);
// Crop the image here.
Log.e("before crop", "w = " + mImageView.getBitmap().getWidth()
+ " h = " + mImageView.getBitmap().getHeight());
// parameters show w = 176 h = 500
// Harry's mods to flow here --------
// Bitmap HarryBitmap = Bitmap.createBitmap(mImageView.getWidth(),mImageView.getHeight(),Bitmap.Config.ARGB_8888);
// Bitmap HarryBitmap = Bitmap.createBitmap(128,500,Bitmap.Config.ARGB_8888);
// Canvas canvas = new Canvas(HarryBitmap);
// mImageView.draw(canvas);
// Bitmap resizedbitmap1 = Bitmap.createBitmap(tempBitmap,24,0,150,500);
// Canvas canvas1 = new Canvas(resizedbitmap1);
// mImageView.draw(canvas1);
/* Try number 2 to approach
Paint paint = new Paint();
paint.setFilterBitmap(true);
int targetWidth = 128;
int targetHeight = 500;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(scanBitmap);
canvas.drawBitmap( scanBitmap, new Rect(24, 0, 176, 500),
new RectF(0, 0, targetWidth, targetHeight), paint);
// Matrix matrix = new Matrix();
// matrix.postScale(1f, 1f);
// Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 128, 500, matrix, true);
//
// //convert Bitmap to resource
// //BitmapDrawable bd = new BitmapDrawable(resizedBitmap);
//
// canvas.drawBitmap( resizedBitmap, new Rect(24, 0, 128, 500),
// new Rect(0, 0, 128, 500), paint);
mImageView.draw(canvas);
*/
Log.e("after crop", "w = " + mImageView.getBitmap().getWidth()
+ " h = " + mImageView.getBitmap().getHeight());
// end Harry's mods to flow --------
// Save the image to the SD card for testing viewing
saveScreen(mImageView.getBitmap());
} catch (IOException e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mImageView.postInvalidate();
}
// Not currently used - Harry July 14 2013
private Bitmap cropBitmap(Bitmap bitmap) {
Bitmap result = Bitmap.createBitmap(bitmap, 1, 1, 128, 500);
Log.e("croped",
"w = " + result.getWidth() + " h = " + result.getHeight());
return result;
}
public void saveScreen(Bitmap bitmap) {
Log.e("saveScreen Enter= " + bitmap.getByteCount(),
"w = " + bitmap.getWidth() + " h =" + bitmap.getHeight());
String root = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/images");
myDir.mkdirs();
String fname = "FPCKit.jpg";
File file = new File(myDir, fname);
/*
* boolean done = false; int n = 1; do { String fname = "Image-" + n +
* ".jpg"; file = new File(myDir, fname); if (!file.exists()) { done =
* true; } n++; } while (!done);
*/
try {
FileOutputStream out = new FileOutputStream(file);
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
// Bitmap bmp = mImageView.getBitmap();
// bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
Log.e("onSave bytes= " + bitmap.getByteCount(),
"w = " + bitmap.getWidth() + " h =" + bitmap.getHeight());
//This will be as previous version. save entire image.
mImageView.getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, out);
// bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Toast toast = Toast.makeText(ImageCaptureActivity.this,
"Image saved to " + file.getAbsolutePath(),
Toast.LENGTH_SHORT);
toast.show();
} catch (Exception e) {
e.printStackTrace();
Toast toast = Toast.makeText(ImageCaptureActivity.this,
"Exception saving image: " + e.getLocalizedMessage(),
Toast.LENGTH_SHORT);
toast.show();
}
}
}