我希望我的代码在使用搜索栏调整图像大小,然后将其保存在android中。我怎样才能做到这一点?

时间:2016-07-22 07:03:44

标签: android

我希望在拍摄之后但在保存之前为图像指定尺寸。我希望搜索栏给图像赋予大小,然后将图像压缩到该大小。因为我是android的新手,所以请帮忙!

我的代码是:

public class ImageActivity extends AppCompatActivity {

private static final int CAMERA_REQUEST = 1888;

private ImageView imageview;
SeekBar sb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);

    sb=(SeekBar)findViewById(R.id.seekbar1);
    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
        int progressChangedValue=0;

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser)
        {
            progressChangedValue = progress;
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar)
        {
            Toast.makeText(ImageActivity.this, "progress starts",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar)
        {
            Toast.makeText(ImageActivity.this, "Seek bar progress is :" + progressChangedValue,
                    Toast.LENGTH_SHORT).show();
        }

    });

    this.imageview = (ImageView) this.findViewById(R.id.imageview1);
    Button photoButton = (Button) this.findViewById(R.id.button1);

    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);

        }
    });
}
protected void onActivityResult(int requestcode, int resultcode, Intent data) {
    if (requestcode == CAMERA_REQUEST && resultcode == RESULT_OK) {

        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageview.setImageBitmap(photo);
        UUID uuid = UUID.randomUUID();
        String randomUUIDString = uuid.toString();

        Toast.makeText(getApplicationContext(),"UUID is"+randomUUIDString,Toast.LENGTH_LONG).show();

    }
}

1 个答案:

答案 0 :(得分:1)

您必须设置最大进度:

seekBar.setMax(200);
// or with the imageview's size: 'seekBar.setMax(imageview.getWidth());'
// if 'getWidth()' returns 0, you need to get it in a thread (with 'post()')

现在,您可以初始化进度(例如,如果您不想要开销200):

seekBar.setProgress(seekBar.getMax());

然后,在侦听器中使用onProgressChange状态,设置新的宽度/高度 假设你有一个等于宽度和高度的图像,最大值为200,你必须这样做:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        imageview.getLayoutParams().width = progress;
        imageview.getLayoutParams().height = progress;
        imageview.requestLayout();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }
});

那就是它!现在,您必须在Bitmap上保存更改的维度,您应该使用ImageView值创建一个新的Bitmap,如下所示:

Bitmap finalBitmap = Bitmap.createScaledBitmap(bitmap,
                    imageview.getWidth(), imageview.getHeight(), false);

这不是最终的解决方案,它需要更多的测试和调整,但它是帮助您获得所需内容的有效线索。